使用JQuery计时器调用js函数。

时间:2022-12-10 08:07:26

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.

但这只执行一次,然后再也不会执行。

8 个解决方案

#1


207  

You can use this:

您可以使用:

window.setInterval(yourfunction, 10000);

function yourfunction() { alert('test'); }

#2


47  

window.setInterval(function() {
 alert('test');
}, 10000);

window.setInterval

setinterval

Calls a function repeatedly, with a fixed time delay between each call to that function.

重复调用一个函数,每次调用该函数之间有一个固定的时间延迟。

#3


45  

Might want to check out jQuery Timer to manage one or multiple timers.

可能需要检查jQuery定时器来管理一个或多个定时器。

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...

#4


25  

setInterval is the function you want. That repeats every x miliseconds.

setInterval是您想要的函数。每x毫秒重复一次。

window.setInterval(function() {
    alert('test');
}, 10000);

#5


11  

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);

http://api.jquery.com/delay/

http://api.jquery.com/delay/

Ooops....my mistake you were looking for an event to continue triggering. I'll leave this here, someone may find it helpful.

糟糕....我的错误是你在找一个事件继续触发。我把这个放在这里,也许有人会觉得有用。

#6


3  

try jQueryTimers, they have great functionality for polling

试试jquerytimer,它们有很好的轮询功能

http://plugins.jquery.com/project/timers

http://plugins.jquery.com/project/timers

#7


1  

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();

#8


-2  

function run() {
    window.setTimeout(
         "run()",
         1000
    );
}

#1


207  

You can use this:

您可以使用:

window.setInterval(yourfunction, 10000);

function yourfunction() { alert('test'); }

#2


47  

window.setInterval(function() {
 alert('test');
}, 10000);

window.setInterval

setinterval

Calls a function repeatedly, with a fixed time delay between each call to that function.

重复调用一个函数,每次调用该函数之间有一个固定的时间延迟。

#3


45  

Might want to check out jQuery Timer to manage one or multiple timers.

可能需要检查jQuery定时器来管理一个或多个定时器。

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...

#4


25  

setInterval is the function you want. That repeats every x miliseconds.

setInterval是您想要的函数。每x毫秒重复一次。

window.setInterval(function() {
    alert('test');
}, 10000);

#5


11  

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);

http://api.jquery.com/delay/

http://api.jquery.com/delay/

Ooops....my mistake you were looking for an event to continue triggering. I'll leave this here, someone may find it helpful.

糟糕....我的错误是你在找一个事件继续触发。我把这个放在这里,也许有人会觉得有用。

#6


3  

try jQueryTimers, they have great functionality for polling

试试jquerytimer,它们有很好的轮询功能

http://plugins.jquery.com/project/timers

http://plugins.jquery.com/project/timers

#7


1  

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();

#8


-2  

function run() {
    window.setTimeout(
         "run()",
         1000
    );
}