Javascript 在 jQuery 中每 5 秒调用一次函数的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2170923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the easiest way to call a function every 5 seconds in jQuery?
提问by ensnare
JQuery, how to call a function every 5 seconds.
JQuery,如何每 5 秒调用一次函数。
I'm looking for a way to automate the changing of images in a slideshow.
我正在寻找一种方法来自动更改幻灯片中的图像。
I'd rather not install any other 3rd party plugins if possible.
如果可能,我宁愿不安装任何其他 3rd 方插件。
回答by Doug Neiner
You don't need jquery for this, in plain javascript, the following will work!
为此,您不需要 jquery,在纯 javascript 中,以下将起作用!
window.setInterval(function(){
/// call your function here
}, 5000);
To stop the loop you can use
要停止循环,您可以使用
clearInterval()
回答by John Boker
you could register an interval on the page using setInterval, ie:
您可以使用 setInterval 在页面上注册一个间隔,即:
setInterval(function(){
//code goes here that will be run every 5 seconds.
}, 5000);
回答by Francisco López-Sancho
Just a little tip for the first answer. If your function is already defined, reference the function but don't call it!!! So don't put any parentheses after the function name. Just like:
只是第一个答案的一点提示。如果您的函数已经定义,请引用该函数但不要调用它!!!所以不要在函数名后面加括号。就像:
my_function(){};
setInterval(my_function,10000);
回答by Ricardo Figueiredo
A good example where to subscribe a setInterval(), and use a clearInterval() to stop the forever loop:
订阅 setInterval() 并使用 clearInterval() 停止永远循环的一个很好的例子:
function myTimer() {
console.log(' each 1 second...');
}
var myVar = setInterval(myTimer, 1000);
call this line to stop the loop:
调用此行以停止循环:
clearInterval(myVar);
回答by Steel Brain
The functions mentioned above execute no matter if it has completed in previous invocation or not, this one runs after every x seconds once the execution is complete
上面提到的函数无论上次调用是否完成都会执行,一旦执行完成,这个函数每隔 x 秒运行一次
// IIFE
(function runForever(){
// Do something here
setTimeout(runForever, 5000)
})()
// Regular function with arguments
function someFunction(file, directory){
// Do something here
setTimeout(someFunction, 5000, file, directory)
// YES, setTimeout passes any extra args to
// function being called
}
回答by Dror
Both setIntervaland setTimeoutcan work for you (as @Doug Neiner and @John Boker wroteboth now point to setInterval).
See herefor some more explanation about both to see which suites you most and how to stop each of them.
双方setInterval并setTimeout能为你的工作(如@Doug Neiner和@约翰博克写无论是现在还是点setInterval)。
请参阅此处了解有关两者的更多说明,以了解您最喜欢的套件以及如何停止它们中的每一个。
回答by erdeepak
you can use window.setInterval and time must to be define in miliseconds, in below case the function will call after every single second (1000 miliseconds)
您可以使用 window.setInterval 并且时间必须以毫秒为单位定义,在以下情况下,该函数将在每一秒(1000 毫秒)后调用
<script>
var time = 3670;
window.setInterval(function(){
// Time calculations for days, hours, minutes and seconds
var h = Math.floor(time / 3600);
var m = Math.floor(time % 3600 / 60);
var s = Math.floor(time % 3600 % 60);
// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML = h + "h "
+ m + "m " + s + "s ";
// If the count down is finished, write some text
if (time < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
time--;
}, 1000);
</script>

