Javascript 使用 JQuery 计时器调用 js 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2295049/
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
Call js-function using JQuery timer
提问by Elitmiar
Is there anyway to implement a timer for JQuery, eg. every 10 seconds it needs to call a js function.
有没有办法为 JQuery 实现一个计时器,例如。每 10 秒它需要调用一个 js 函数。
I tried the following
我尝试了以下
window.setTimeout(function() {
alert('test');
}, 10000);
but this only executes once and then never again.
但这只会执行一次,然后再也不会执行。
回答by Kristof Claes
You can use this:
你可以使用这个:
window.setInterval(yourfunction, 10000);
function yourfunction() { alert('test'); }
回答by rahul
window.setInterval(function() {
alert('test');
}, 10000);
Calls a function repeatedly, with a fixed time delay between each call to that function.
重复调用一个函数,在每次调用该函数之间有一个固定的时间延迟。
回答by jchavannes
Might want to check out jQuery Timerto manage one or multiple timers.
可能想查看jQuery Timer来管理一个或多个计时器。
http://code.google.com/p/jquery-timer/
http://code.google.com/p/jquery-timer/
var timer = $.timer(yourfunction, 10000);
function yourfunction() { alert('test'); }
Then you can control it with:
然后你可以控制它:
timer.play();
timer.pause();
timer.toggle();
timer.once();
etc...
回答by Ikke
setIntervalis the function you want. That repeats every x miliseconds.
setInterval是您想要的功能。每 x 毫秒重复一次。
window.setInterval(function() {
alert('test');
}, 10000);
回答by Craig
jQuery 1.4 also includes a .delay( duration, [ queueName ] ) method if you only need it to trigger once and have already started using that version.
jQuery 1.4 还包括一个 .delay( duration, [ queueName ] ) 方法,如果您只需要它触发一次并且已经开始使用该版本。
$('#foo').slideUp(300).delay(800).fadeIn(400);
Ooops....my mistake you were looking for an event to continue triggering. I'll leave this here, someone may find it helpful.
哎呀......我的错误,你正在寻找一个事件来继续触发。我会把这个留在这里,有人可能会觉得它有帮助。
回答by Eggie
try jQueryTimers, they have great functionality for polling
试试 jQueryTimers,它们有很好的轮询功能
回答by Aren Hovsepyan
You can use setInterval()method also you can call your setTimeout()from your custom function for example
例如,您可以使用setInterval()方法,也可以 从自定义函数调用setTimeout()
function everyTenSec(){
console.log("done");
setTimeout(everyTenSec,10000);
}
everyTenSec();
回答by harpax
function run() {
window.setTimeout(
"run()",
1000
);
}

