clearTimeout和clearInterval是一样的吗? [英] Are clearTimeout and clearInterval the same?
问题描述
在处理Web应用程序的某些Javascript时,我注意到我使用了 setTimeout
,但我试图用 clearInterval <清除它/ code>并且它停止了Google Chrome和Internet Explorer 9中发生的超时。
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.
clearTimeout
和 clearInterval
可互换?
Here's a JSfiddle with an example of what I'm talking about.
推荐答案
不,他们不可互换。
当然,有些浏览器可能会以相同的方式共享相同的代码以清除间隔和超时,但并不意味着它们可互换并且您肯定不能保证它们在所有浏览器实现中都能正常工作。归结为这两种方法的定义不同,因为不同的目的,因此你应该将它们用于指定的用途。否则,你只是在寻找麻烦。
Sure, some browsers may very well share the same code to clear intervals and timeouts the same way, but does not mean they are interchangeable and 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.
这篇关于clearTimeout和clearInterval是一样的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!