javascript clearTimeout 和 clearInterval 是一样的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9913719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are clearTimeout and clearInterval the same?
提问by Ivan
When working on some Javascript for a web application, I noticed that I had used setTimeout
, but I had tried to clear it with clearInterval
and it stopped the timeout from occurring in Google Chrome and Internet Explorer 9.
在为 Web 应用程序处理一些 Javascript 时,我注意到我使用了setTimeout
,但我试图用 清除它,clearInterval
它阻止了 Google Chrome 和 Internet Explorer 9 中发生的超时。
Are clearTimeout
and clearInterval
interchangeable?
是clearTimeout
和clearInterval
互换?
Here's a JSfiddlewith an example of what I'm talking about.
这是一个 JSfiddle,其中包含我正在谈论的示例。
采纳答案by rgthree
No, they are not interchangeable.
不,它们不可互换。
Sure, some browsers may very well share the same code to clear intervals and timeouts the same way, but does not mean they are interchangeableand you are most certainly not guaranteed that they would work the same across all browser implementations. It comes down to these two methods being defined differently for different purposes and therefore you should use them for their designated uses.. Otherwise, you're just asking for trouble.
当然,某些浏览器很可能会共享相同的代码来以相同的方式清除间隔和超时,但这并不意味着它们是可互换的,而且您肯定不能保证它们在所有浏览器实现中都能正常工作。这归结为这两种方法的定义不同用于不同的目的,因此您应该将它们用于指定的用途。否则,您只是在自找麻烦。
回答by lemiant
Actually I believe that we can make a fairly strong conclusion from the W3C spec (http://www.w3.org/TR/html5/webappapis.html#timers). It is not explicitly guaranteed but we have a lot of evidence that almost any reasonable implementation would have this behavior:
实际上,我相信我们可以从 W3C 规范 ( http://www.w3.org/TR/html5/webappapis.html#timers) 中得出一个相当有力的结论。没有明确保证,但我们有很多证据表明几乎任何合理的实现都会有这种行为:
1) Timeouts and Intervals actually use the same underlying function:
1)超时和间隔实际上使用相同的底层函数:
The setTimeout() method must return the value returned by the timer initialization steps, passing them the method's arguments... and the repeat flag set to false.
The setInterval() method must return the value returned by the timer initialization steps, passing them the method's arguments.... and the repeat flag set to true.
setTimeout() 方法必须返回计时器初始化步骤返回的值,将方法的参数传递给它们……并且将重复标志设置为 false。
setInterval() 方法必须返回由计时器初始化步骤返回的值,将方法的参数传递给它们......并且将重复标志设置为 true。
2) This single function - the "timer initialization steps" mentioned above - uses a single list of timers:
2)这个单一的函数——上面提到的“定时器初始化步骤”——使用了一个定时器列表:
2, ...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.
10, Return handle...
2, ...让句柄是一个用户代理定义的大于零的整数,它将标识此调用在活动计时器列表中设置的超时时间。
10、返回手柄...
3) clearTimeout() and clearInterval() both operate on that list (and in fact are not differentiated by the spec in any way)
3) clearTimeout() 和 clearInterval() 都在该列表上操作(实际上,规范没有以任何方式区分)
The clearTimeout() and clearInterval() methods must clear the entry identified as handle from the list of active timers of the WindowTimers object on which the method was invoked, where handle is the argument passed to the method, if any. (If handle does not identify an entry in the list of active timers of the WindowTimers object on which the method was invoked, the method does nothing.)
clearTimeout() 和 clearInterval() 方法必须从调用该方法的 WindowTimers 对象的活动计时器列表中清除标识为 handle 的条目,其中 handle 是传递给该方法的参数(如果有)。(如果句柄未标识调用该方法的 WindowTimers 对象的活动计时器列表中的条目,则该方法不执行任何操作。)
I believe this presents a fairly strong case that clearTimeout and clearInterval should by synonymous according to the spec. This is backed up by the fact that this works in all the browsers I tested (Chrome 37, Firefox 33, and Opera 25).
我相信这是一个相当有力的例子,根据规范, clearTimeout 和 clearInterval 应该是同义词。这是因为它适用于我测试的所有浏览器(Chrome 37、Firefox 33 和 Opera 25)。
回答by Tom Auger
From the Mozilla reference:
It's worth noting that the pool of IDs used by setTimeout() and setInterval() are shared, which means you can technically use clearTimeout() and clearInterval() interchangeably. However, for clarity, you should avoid doing so.
值得注意的是,setTimeout() 和 setInterval() 使用的 ID 池是共享的,这意味着您可以在技术上交替使用 clearTimeout() 和 clearInterval()。但是,为清楚起见,您应该避免这样做。
回答by YMMD
Even if they can be used synonymously now it could change at any time in the future. Why shouldn't you call a spade a spade? :-)
即使它们现在可以作为同义词使用,但将来也可能随时更改。你为什么不应该直言不讳?:-)