Home > Article > Web Front-end > A brief discussion on the setInterval() method in jQuery_javascript skills
Definition and usage:
The setInterval() method can call a function or calculate an expression according to the specified period (in milliseconds).
The setInterval() method will keep calling the function until clearInterval() is called or the window is closed. The ID value returned by setInterval() can be used as an argument to the clearInterval() method.
var time=0;
Usage 1:
function jump(){ ………… //函数内容 } time = setInterval("jump",5000); //每个五秒调用一次函数
When you need to pause
$("").hover(function(){ clearInterval(time),function(){ time = setInterval("jump",5000); } })
Usage 2:
function autoPlay(){ time = setInterval(function(){ ………… //函数内容 },5000); } autoPlay(); //调用函数
When you need to pause
$("").hover(function(){ clearInterval(time),function(){ autoPlay(); } })
Summary:
The first usage idea is clearer. First set up a function and call it yourself through setInterval, but it is more difficult to call it elsewhere;
The second method seems messy. Write the self-calling function inside setInterval, then attach a famous function to it, and then implement automation by calling the famous function. It is more convenient to call it elsewhere.
The above is purely my personal opinion, I hope the experts can give me some advice.