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.
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);
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);
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.
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);
Similarly, the setTimeout() method also returns a timer ID, and the timer can be cleared through the clearTimeout() method.
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>
.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; }
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); });
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!