本文介绍了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演示:
推荐答案
请考虑使用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
}
这篇关于javascript计时器循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!