admin管理员组文章数量:1291721
The following code is taken from Project Silk (a Microsoft sample application) The publish method below loops thru an an array of event callBacks and executes each one. Instead of using a for loop setInterval is used instead.
The documentation says this allows each subscriber callback to be invoked before the previous callback finishes. Is this correct? I thought the browser would not allow the execution of the function inside the interval to run until all prior executions of it had finished.
Is this really any different than doing a for loop?
that.publish = function (eventName, data)
{
var context, intervalId, idx = 0;
if (queue[eventName])
{
intervalId = setInterval(function ()
{
if (queue[eventName][idx])
{
context = queue[eventName][idx].context || this;
queue[eventName][idx].callback.call(context, data);
idx += 1;
}
else { clearInterval(intervalId); }
}, 0);
}
The following code is taken from Project Silk (a Microsoft sample application) The publish method below loops thru an an array of event callBacks and executes each one. Instead of using a for loop setInterval is used instead.
The documentation says this allows each subscriber callback to be invoked before the previous callback finishes. Is this correct? I thought the browser would not allow the execution of the function inside the interval to run until all prior executions of it had finished.
Is this really any different than doing a for loop?
that.publish = function (eventName, data)
{
var context, intervalId, idx = 0;
if (queue[eventName])
{
intervalId = setInterval(function ()
{
if (queue[eventName][idx])
{
context = queue[eventName][idx].context || this;
queue[eventName][idx].callback.call(context, data);
idx += 1;
}
else { clearInterval(intervalId); }
}, 0);
}
Share
Improve this question
edited Feb 17, 2012 at 18:29
C.J.
asked Feb 17, 2012 at 6:06
C.J.C.J.
6,8797 gold badges37 silver badges45 bronze badges
4
- Are you looking for something like this: github./caolan/async#parallel – Syed Aslam Commented Feb 17, 2012 at 6:45
-
I'm interested in understanding if this code does what the documentation claims it does. Because if it does there's something I don't understand about the way
setInterval
works. I don't understand it, especially since JS is single threaded. – C.J. Commented Feb 17, 2012 at 6:54 -
2
The
setInterval(..., 0)
callbacks won't be triggered until the browser has control again so thesetInterval
calls are pretty much saying "browser, once I'm done, call this function". – mu is too short Commented Feb 17, 2012 at 9:13 - That was my thinking too. So why would you do this over a simple for loop? – C.J. Commented Feb 17, 2012 at 14:41
2 Answers
Reset to default 4Using setInterval here does make the execution sort of "asynchronous", because it schedules the execution of the callback for the next time the main execution thread is available.
This means that the callback executions should not block the browser because any other synchronous processing will take place before the callbacks (because the callbacks are scheduled to run only when the main execution thread has a spare millisecond) - and that's what makes this construct "better" than a regular for loop - the fact that the callbacks won't block the browser and cause the dreaded "This page has a script that's taking too long" error.
The side effect of this scheduling pattern is that the timeout is only a "suggestion" - which is why they use 0 here.
See: http://ejohn/blog/how-javascript-timers-work/
setInterval(..., 0)
can be used to yield control to the browser UI, to prevent it from freezing if your code takes a long time to run.
In this case, that.publish
will exit almost immediately before any callback has been executed. Each callback will then run "in the background" - they will be placed in the event loop, and each one will yield to the browser to do it's thing before the next callback is executed.
This seems like a good idea in event processing, because you don't want event processing to freeze the browser even if there are a lot of them or some take a long time to finish.
About the documentation - as stated, it is incorrect. Javascript is single-threaded. But if you were to call publish()
a few times in a row, it is true that those calls would all finish before any callbacks are executed, because of the setTimeout
. Maybe this is what the documentation means?
本文标签: javascriptCan you use setInterval to call functions asynchronouslyStack Overflow
版权声明:本文标题:javascript - Can you use setInterval to call functions asynchronously? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741533295a2383883.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论