全部:
我想知道是否有一种方法可以强制清除所有间隔计数器,例如:
/* var counter = */ setInterval(function(){console.log("Tick")}, 1000)
但我忘记获取引用(那个 counter
变量,如果在控制台中打印出来,它是一个数字),我想知道是否有办法找到 Interval 是什么数字,所以我可以调用 clearInterval
最佳答案
关于 id 唯一可以确定的是它将是一个大于零的整数。参见 the spec .
let handle be a user-agent-defined integer that is greater than zero that will identify the timeout to be set by this call in the list of active timers.
所以你可以非常低效地尝试所有这些:
for (let i = 1; i < Number.MAX_SAFE_INTEGER; i++) {
clearInterval(i);
}
请注意,这将需要一段时间(有很多数字需要循环,尽管您可能假设浏览器会按顺序发出它们,并且您永远不会生成超过一定数量的数字)。
另请注意(See MDN):
the pool of IDs used by setInterval() and setTimeout() are shared, which means you can technically use clearInterval() and clearTimeout() interchangeably
所以这将关闭所有 setTimeout
调用以及所有 setInterval
调用。
关于javascript - 如何强制清除Javascript中的间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54013616/