再次 setTimeout 与 setInterval

发布于 2024-12-12 00:13:02 字数 490 浏览 2 评论 0原文

所以我知道 setTimeoutsetInterval 之间存在差异,但请考虑这两个代码示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2024-12-19 00:13:02

它们在功能上大致相同,但也存在差异。一个区别在于,如果 doSomething 花费的时间比间隔长,浏览器如何处理它。使用setInterval,如果doSomething仍在运行,至少某些浏览器将跳过下一个间隔。因此,如果您使用 100 毫秒,并且 doSomething 需要 110 毫秒来运行,则下一次运行要等到 90 毫秒后才会发生(当然,所有这些时间都是近似值)。

另一个区别是,使用 setTimeout 时,您每次都会获得一个新句柄,而使用 setInterval 时,您只会获得一个句柄。

与给出的示例的另一个区别是,在 setTimeout 示例中,您每次都会启动 JavaScript 解析器/编译器,而使用 setInterval 你只启动解析器/编译器一次。但这种差异并不重要,因为您根本不应该这样做 - 见下文。

但抛开细微差别不谈,你所拥有的功能在功能上是相同的。


旁注:将字符串传递到 setTimeoutsetInterval 中都不是最佳实践。相反,传入一个函数引用:

// setTimeout
function myFunction(){
   setTimeout(myFunction, 100);
   doSomething();
}
setTimeout(myFunction, 100);

// setInterval
function myFunction(){
   doSomething();
}
setInterval(myFunction, 100);

传入一个字符串会启动 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. With setInterval, at least some browsers will just skip the next interval if doSomething is still running. So if you use 100ms as you have, and doSomething 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 with setInterval 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 with setInterval 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 or setInterval. Instead, pass in a function reference:

// setTimeout
function myFunction(){
   setTimeout(myFunction, 100);
   doSomething();
}
setTimeout(myFunction, 100);

// setInterval
function myFunction(){
   doSomething();
}
setInterval(myFunction, 100);

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).

北方的韩爷 2024-12-19 00:13:02

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文