如何在javascript中设置计时器

时间:2022-03-17 02:48:49

I want to run the following code:

我想运行以下代码:

ajaxUpdate(10);

With a delay of 1 second between each iteration. How can I do this?

每次迭代间隔1秒。我该怎么做呢?

6 个解决方案

#1


6  

You can also do it with

你也可以这么做

setTimeout(function() {ajaxUpdate(10)}, 1000);

#2


39  

var i = window.setInterval( function(){ 
          ajaxUpdate(10); 
 }, 1000 ); 

This will call ajaxUpdate every second, until such a time it is stopped.

这将每秒钟调用一次ajaxUpdate,直到此时间停止。

And if you wish to stop it later:

如果你以后想停止的话:

window.clearInterval( i ); 

If you wish to only run it once however,

如果你只希望运行一次,

var i = window.setTimeout( function(){ 
          ajaxUpdate(10); 
 }, 1000 ); 

Will do the trick, and if you want to stop it running before it gets around to running once

会起作用的,如果你想在它开始运行之前停止运行

window.clearTimeout(i); 

The "window" prefix is not strictly nessecary, but its a good idea, because you never know when somebody else might like to create something else with the same name in visible scope that behaves differently.

“窗口”前缀并不是严格意义上的nessecary,但它是一个好主意,因为您永远不知道什么时候其他人可能想要在可见范围内创建具有相同名称的、行为不同的东西。

For a complete reference on this, I always find MDC Very Helpful:

对于这方面的完整参考,我总是发现MDC非常有用:

Also, you may wish to read this article on timers by John Resig,

另外,您可能希望阅读John Resig关于定时器的文章,

#3


5  

You can use setInterval() for that. Create an anonymous function to be called, and use the time in milliseconds:

您可以为此使用setInterval()。创建要调用的匿名函数,并使用以毫秒为单位的时间:

var myInterval = window.setInterval(function() { ajaxUpdate(10); }, 1000);

#4


1  

You can use this JavaScript Timer class.

您可以使用这个JavaScript定时器类。

#5


0  

You can use too jQuery Timers: http://plugins.jquery.com/project/timers

您可以使用jQuery定时器:http://plugins.jquery.com/project/timer

#6


-1  

You can use the function setTimeout(String fonc, Integer delay). For example, to execute your code each second you can do :

可以使用函数setTimeout(String fonc, Integer delay)。例如,要每秒钟执行一次代码,您可以做到:

window.setTimout("ajaxUpate",100);

Hope i answer to your question ;)

希望我能回答你的问题。

#1


6  

You can also do it with

你也可以这么做

setTimeout(function() {ajaxUpdate(10)}, 1000);

#2


39  

var i = window.setInterval( function(){ 
          ajaxUpdate(10); 
 }, 1000 ); 

This will call ajaxUpdate every second, until such a time it is stopped.

这将每秒钟调用一次ajaxUpdate,直到此时间停止。

And if you wish to stop it later:

如果你以后想停止的话:

window.clearInterval( i ); 

If you wish to only run it once however,

如果你只希望运行一次,

var i = window.setTimeout( function(){ 
          ajaxUpdate(10); 
 }, 1000 ); 

Will do the trick, and if you want to stop it running before it gets around to running once

会起作用的,如果你想在它开始运行之前停止运行

window.clearTimeout(i); 

The "window" prefix is not strictly nessecary, but its a good idea, because you never know when somebody else might like to create something else with the same name in visible scope that behaves differently.

“窗口”前缀并不是严格意义上的nessecary,但它是一个好主意,因为您永远不知道什么时候其他人可能想要在可见范围内创建具有相同名称的、行为不同的东西。

For a complete reference on this, I always find MDC Very Helpful:

对于这方面的完整参考,我总是发现MDC非常有用:

Also, you may wish to read this article on timers by John Resig,

另外,您可能希望阅读John Resig关于定时器的文章,

#3


5  

You can use setInterval() for that. Create an anonymous function to be called, and use the time in milliseconds:

您可以为此使用setInterval()。创建要调用的匿名函数,并使用以毫秒为单位的时间:

var myInterval = window.setInterval(function() { ajaxUpdate(10); }, 1000);

#4


1  

You can use this JavaScript Timer class.

您可以使用这个JavaScript定时器类。

#5


0  

You can use too jQuery Timers: http://plugins.jquery.com/project/timers

您可以使用jQuery定时器:http://plugins.jquery.com/project/timer

#6


-1  

You can use the function setTimeout(String fonc, Integer delay). For example, to execute your code each second you can do :

可以使用函数setTimeout(String fonc, Integer delay)。例如,要每秒钟执行一次代码,您可以做到:

window.setTimout("ajaxUpate",100);

Hope i answer to your question ;)

希望我能回答你的问题。