再次 setTimeout 与 setInterval
所以我知道 setTimeout
和 setInterval
之间存在差异,但请考虑这两个代码示例:
function myFunction(){
setTimeout('myFunction();', 100);
doSomething();
}
setTimeout('myFunction();', 100);
并且
function myFunction(){
doSomething();
}
setInterval('myFunction();', 100);
请注意,在第一个示例中我调用 setTimeout
在函数的开头,然后我doSomething
。因此,doSomething()
不会产生额外的延迟。这是否意味着这两个示例的作用完全相同?还是还有更细微的差别?
So I know that there are differences between setTimeout
and setInterval
, but consider these two code examples:
function myFunction(){
setTimeout('myFunction();', 100);
doSomething();
}
setTimeout('myFunction();', 100);
and
function myFunction(){
doSomething();
}
setInterval('myFunction();', 100);
Note that in the first example I call setTimeout
at the begining of the function and then I doSomething
. Therefore there is no extra delay from doSomething()
. Does that mean that those two examples do exactly the same? Or is there even more subtle difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们在功能上大致相同,但也存在差异。一个区别在于,如果
doSomething
花费的时间比间隔长,浏览器如何处理它。使用setInterval
,如果doSomething
仍在运行,至少某些浏览器将跳过下一个间隔。因此,如果您使用 100 毫秒,并且doSomething
需要 110 毫秒来运行,则下一次运行要等到 90 毫秒后才会发生(当然,所有这些时间都是近似值)。另一个区别是,使用
setTimeout
时,您每次都会获得一个新句柄,而使用setInterval
时,您只会获得一个句柄。与给出的示例的另一个区别是,在
setTimeout
示例中,您每次都会启动 JavaScript 解析器/编译器,而使用setInterval
你只启动解析器/编译器一次。但这种差异并不重要,因为您根本不应该这样做 - 见下文。但抛开细微差别不谈,你所拥有的功能在功能上是相同的。
旁注:将字符串传递到
setTimeout
或setInterval
中都不是最佳实践。相反,传入一个函数引用:传入一个字符串会启动 JavaScript 解析器并执行与
eval
相同的操作。应尽可能避免(而且几乎总是可能的)。They're functionally about the same, but there are differences. One difference is in how browsers handle it if
doSomething
takes longer than the interval. WithsetInterval
, at least some browsers will just skip the next interval ifdoSomething
is still running. So if you use 100ms as you have, anddoSomething
takes 110 ms to run, the next run won't happen until 90ms later (of course, all of these times are approximate).Another difference is that with the
setTimeout
, you'll get a new handle every time, whereas withsetInterval
you get one handle.Another difference with your examples as given is that in the
setTimeout
example, you're firing up a JavaScript parser/compiler every time, whereas withsetInterval
you're only firing up the parser/compiler once. But this difference shouldn't matter, because you shouldn't be doing that at all — see below.But subtleties aside, what you have there is functionally the same.
Side note: It's not best practice to pass strings into either
setTimeout
orsetInterval
. Instead, pass in a function reference:Passing in a string fires up a JavaScript parser and does the same thing as
eval
. It should be avoided whenever possible (and it's almost always possible).TJ Crowder 解释了主要差异,可能会出现另一个更微妙的差异(我更改了时间尺度,因为它更容易解释):
让我们绘制一个非常大的超时时间的差异:1 天。您在第一天的 00:00 调用这两个方法并让它运行 1 年...
1 年后,您通过 setInterval 调用的方法将在 00:00 + 一些毫秒执行(因为您可能不是唯一一个要求处理器在此时执行操作,并且操作系统计时器无论如何都有粒度)。
但是你的 setTimeout 方法会在稍后发生,可能在 00:01 左右,因为每天它都会在请求的时间之后调用一点,并请求在第二天的同一时间调用...
PS:它也可以在某些情况下会在请求的时间之前调用,但通常会在 :-D 之后运行
T.J. Crowder explained the main differences, one other more subtle could appear (I change the time scale as it's easier to explain) :
Lets plot the difference with a very big timeout time : 1 Day. You call both methods at 00:00 on Day 1 and let it run for 1 year...
1 Year latter your method called by setInterval will execute at 00:00 + some milliseconds (because you may not be the only one asking for the processors to do things at this exact moment and the OS timers have granularity anyway).
But your setTimeout method will occur latter, maybe around 00:01 because each day it would have been called a little after the requested time and requested to be called the next day at the same time...
PS: It could also be called before the requested time in some cases but more often than not it run after :-D