javascript计时器循环

javascript计时器循环

问题描述:

我想创建一个计时器,一旦达到某个点,计时器就会重置,然后重新开始。

I want to create a timer that once it reaches a certain point, the timer resets, and then starts over.

现在,我已经得到了循环设置,作为测试,我希望它在5000毫秒(5秒)后重置。但计数器全部乱了。

Right now, I've got the loop set up, and as a test I want it to reset after 5000 ms (5 seconds). But the counter goes all haywire.

WIP演示: http://jsfiddle.net/stursby/wUHA3/

请考虑使用setInterval而不是setTimeout。它将自动重复,直到你清除间隔。

Instead of setTimeout, consider using setInterval. It will repeat automatically until you clear the interval.

setInterval(myMethod, 5000);

function myMethod( )
{
  //this will repeat every 5 seconds
  //you can reset counter here
}