浏览器是否跟踪活动计时器 ID?
浏览器是否跟踪活动的 setInterval
和 setTimeout
ID?或者这完全取决于开发人员来跟踪?
如果它确实跟踪它们,是否可以通过 BOM 访问?
Does the browser keep track of active setInterval
and setTimeout
IDs? Or is this solely up to the developer to keep track of?
If it does keep track of them, is it accessible via the BOM?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由开发人员来跟踪。您可以通过使用 setTimeout/setInterval 函数的返回值并将该值传递给clearTimeout/clearInterval 函数来完成此操作 - 如此处其他答案中所述。
这似乎是因为每个浏览器都会以自己的方式实现跟踪间隔。
来自 w3.org/TR/2009/WD-html5-20090212/no.html (草案,但 w3schools 和 http ://w3.org/TR/Window 以几乎相同的方式解释它) - setTimeout 和 setInterval 返回一个 long 值,而clearTimeout/clearInterval 接受一个 long 值来查找和取消
It is up for the developer to keep track of. You can do so by using the returned value of the setTimeout/setInterval function and passing that value to the clearTimeout/clearInterval function - as described in other answers here.
This appears to be because each browser will implement keeping track of the intervals in their own way.
From w3.org/TR/2009/WD-html5-20090212/no.html (a draft, but w3schools and http://w3.org/TR/Window explain it almost the same way) - setTimeout and setInterval return a long and clearTimeout/clearInterval accept a long to find and cancel
您可以通过覆盖
setTimeout
/seInterval
函数来添加此类全局计时器跟踪。作为奖励,您可以在设置或弹出计时器时轻松添加代码,跟踪实时计时器或弹出计时器等...例如:
You can add such global timers tracking by overriding the
setTimeout
/seInterval
functions. As a bonus you easily add code when a timer is set or popped, track live timers or popped timers, etc...For example:
如果您对计时器的窗口如何“记住”计时器感到好奇,您可能会对这一点感兴趣。
This may interest you, if you are curious about how the timer is 'remembered' by its window.
更新:
这个问题有两个方面。
我只能假设#1(以及后来的#2)OP 的意思是一般意义上的“它们是否被跟踪”,因为作为开发人员,他/她希望控制它们。
简而言之,是的,它们会被跟踪(正如 @s_hewitt 所指出的,浏览器将其作为
long
值),并且开发人员可以通过在设置时维护对计时器的引用来管理它们。作为开发人员,您可以通过调用 (clearInterval(handleRef) 或 clearTimeout(handleRef))
但是没有默认值
window.timers
或类似的集合,为您提供现有计时器的列表 - 如果您觉得需要,您将需要自己维护它。您只需创建一个对计时器的变量引用,并且如果/需要时,按名称清除它。
当您加载另一个页面时,所有计时器都会被浏览器自动清除,因此您不需要维护句柄,并清除它们,除非您需要/想要。
Update:
There are 2 aspects to this question.
I can only presume for #1 (and later #2) that the OP means "are they tracked" in the general sense because as a Developer s/he would like control over them.
In short, yes they are tracked (as @s_hewitt noted, as
long
values by the browser) and they can be managed by the developer by maintaining a reference to the timers when setup.As a developer you can control (e.g. stop) them by calling (clearInterval(handleRef), or clearTimeout(handleRef))
However there is no default
window.timers
or similar collection that gives you a list of the existing timers - you will need to maintain that yourself if you feel you need to.You simply need to create a variable reference to your timer, and if/when needed, clear it by name.
When you load another page, all the timers are automatically cleared by the browser so you don't need to maintain a handle, and clear them unless you need/want to.
看下面的脚本,浏览器可以记住每个 setTimeout 迭代的 id
您可以通过以下方式访问它们
Look at the scripts below, the browser could remember the id of each setTimeout iteration
You can access them by