JavaScript setInterval 限制?
我有一个使用 JavaScript 的 setInterval()
来运行数字时钟的应用程序。我想知道它执行此函数的次数是否有超时或限制。
I have an application using JavaScript's setInterval()
to run a digital clock. I was wondering if it has a timeout, or limit, to the amount of times it can execute this function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
setInterval()
将无限运行。如果您想终止“循环”,可以使用clearInterval。例如:
setInterval()
will run infinitely.If you wish to terminate the 'loop' you can use clearInterval. For example:
否,给定的函数将继续执行,直到您使用
clearInterval()
手动清除间隔。请注意,在大多数浏览器中,当页面位于后台选项卡中时,您的函数仍将执行,但移动浏览器(特别是 IOS5 Safari)可能会释放页面,直到其再次聚焦/可见。
No, the given function will keep being executed until you clear the interval manually with
clearInterval()
Note that in most browsers, your function will still be executed when the page is in a background tab, but mobile browsers (notably IOS5 Safari) may free the page until its focused / visible again.
正如其他人提到的,间隔运行的次数没有限制,但是如果您打算无限期地运行计时器,您应该考虑这里的信息:
后台选项卡上的最小 setInterval()/setTimeout() 延迟
如果您的用户可能会退出, 1 秒似乎是安全的最小间隔
As others have mentioned, there is not limit to the number of times your interval will run, however if you intend to run a timer indefinitely you should consider the info here:
Minimum setInterval()/setTimeout() delay on background tabs
If your user is likely to tab-out, 1 second seems to be safe minimum interval