We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
在用setInterval的时候,我们这么用:
setInterval
var num = 0; var max = 10; var timer = undefined; function fn () { num ++; if (num === max) { clearInterval(timer); alert('完成'); } } timer = setInterval(fn, 1000);
但是有一个问题,就是可能我们的下一个定时器会在上一个定时器还没有结束前就启动,那么在清除定时器的时候,timer就会出问题。 所以我们应该规避这个问题。
可以用setTimeout来实现同样的功能来避免:
setTimeout
var num = 0; var max = 10; function fn () { num ++; if (num < max) { setTimeout(fn, 1000); } else { alert('完成'); } }
省去了清除计时器ID的问题,避免了上述问题。
所以通常用setTimeout代替setInterval
The text was updated successfully, but these errors were encountered:
No branches or pull requests
在用
setInterval
的时候,我们这么用:但是有一个问题,就是可能我们的下一个定时器会在上一个定时器还没有结束前就启动,那么在清除定时器的时候,timer就会出问题。 所以我们应该规避这个问题。
可以用
setTimeout
来实现同样的功能来避免:省去了清除计时器ID的问题,避免了上述问题。
所以通常用
setTimeout
代替setInterval
The text was updated successfully, but these errors were encountered: