admin管理员组文章数量:1296464
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 beginning 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?
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 beginning 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?
2 Answers
Reset to default 11They'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/piler every time, whereas with setInterval
you're only firing up the parser/piler 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).
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
本文标签:
版权声明:本文标题:javascript - Is there a difference between passing a function to `setInterval` once and repeatedly passing it to `setTimeout`? - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741638447a2389775.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论