How to implement timer in jquery

WBOY
Release: 2023-05-25 09:43:37
Original
3247 people have browsed it

JQuery is a very popular JavaScript library that provides us with many convenient functions to operate DOM, event handling, animation effects, and more. Among them, timer is also a very important function in JQuery. We can set timer to realize dynamic update interface, carousel chart and other functions. Next, this article will introduce in detail how to use JQuery to implement the timer function.

  1. setInterval() method

The setInterval() method is provided in JQuery to implement the timer function. This method receives two parameters: the first parameter is the function to be executed, and the second parameter is the execution interval in milliseconds. For example, we can use the following code to pop up a prompt box every 1 second:

setInterval(function(){
    alert("Hello World!");
}, 1000);
Copy after login

It should be noted that the setInterval() method returns a timer ID, and we can use this ID to clear the timer. For example, the following code can be used to stop the above timer after 2 seconds:

var timerID = setInterval(function(){
    alert("Hello World!");
}, 1000);
setTimeout(function(){
    clearInterval(timerID);
}, 2000);
Copy after login

The setTimeout() method is used here to implement the operation of stopping the timer with a delay of 2 seconds. The clearInterval() method can clear the timer created by the setInterval() method.

  1. setTimeout() method

In addition to the setInterval() method, JQuery also provides the setTimeout() method, which is used to execute a specified time after a certain period of time. function. The setTimeout() method also receives two parameters: the first parameter is the function to be executed, and the second parameter is the execution interval in milliseconds. For example, the following code can pop up a prompt box after 3 seconds:

setTimeout(function(){
    alert("Hello World!");
}, 3000);
Copy after login

Similarly, the setTimeout() method also returns a timer ID, and the timer can be cleared through the clearTimeout() method.

  1. Implementing Carousel Charts

The above introduces the two timer methods in JQuery. We can use these methods to implement some common functions, such as carousel charts. . Let’s introduce a simple carousel chart implementation method.

<div class="slider">
    <ul>
        <li><img src="image1.jpg"></li>
        <li><img src="image2.jpg"></li>
        <li><img src="image3.jpg"></li>
    </ul>
</div>
Copy after login
.slider {
    width: 500px;
    height: 300px;
    overflow: hidden;
}
.slider ul {
    list-style: none;
    width: 1500px;
    height: 300px;
    margin: 0;
    padding: 0;
}
.slider li {
    float: left;
    width: 500px;
    height: 300px;
}
Copy after login
var index = 0;
var timerID = setInterval(function(){
    index++;
    if(index > 2){
        index = 0;
    }
    $(".slider ul").animate({
        left: -index * 500 + "px"
    }, 500);
}, 2000);

$(".slider").hover(function(){
    clearInterval(timerID);
}, function(){
    timerID = setInterval(function(){
        index++;
        if(index > 2){
            index = 0;
        }
        $(".slider ul").animate({
            left: -index * 500 + "px"
        }, 500);
    }, 2000);
});
Copy after login

This code implements a carousel image that cycles every 2 seconds. When the mouse moves into the carousel, the timer is cleared and restarts when the mouse moves out. The implementation principle of this carousel picture is very simple. It uses timers and animation effects to set a cycle number and cycle interval to switch pictures every once in a while.

In short, the timer is a very commonly used function in JQuery. It can achieve some dynamic effects and carousel graphics and other functions. We can use the setInterval() or setTimeout() method to implement the timer. At the same time, we must pay attention to the management and clearing of the timer ID to avoid memory leaks.

The above is the detailed content of How to implement timer in jquery. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template