admin管理员组文章数量:1299997
I'm writing a ubiquity plugin the long function callback for an ajax query is blocking up the GUI Thread causing firefox to lock up.
The obvious solution seem to be to use some sort of deferred execution of (i.e we want to periodically add the carry out doing this query function to the end of the event queue and then allow other mands to be carried out.
The only way I can think of doing this is to use settimeout with a timeout of zero... is this is guaranteed to work, or is there a better way of doing this.
I'm writing a ubiquity plugin the long function callback for an ajax query is blocking up the GUI Thread causing firefox to lock up.
The obvious solution seem to be to use some sort of deferred execution of (i.e we want to periodically add the carry out doing this query function to the end of the event queue and then allow other mands to be carried out.
The only way I can think of doing this is to use settimeout with a timeout of zero... is this is guaranteed to work, or is there a better way of doing this.
Share Improve this question asked Sep 20, 2009 at 21:31 user47741user47741 3,0154 gold badges23 silver badges14 bronze badges 1- How are you doing the AJAX? If you're loading a script tag, that could be blocking (browser blocks until the script es back). – Steve Brewer Commented Sep 21, 2009 at 4:16
2 Answers
Reset to default 8Using setTimeout
with a very small timeout (0
or very nearly zero if you're feeling paranoid) is the only way to do this in a browser context. It works very well and is very reliable, but be sure to yield often enough but not too often, as it does take a while to e back to you ("a while" in a puter sense, of course; it's almost instantaneous [modulo other things you may be doing] in human terms).
Make sure you are using an asynchronous request as a synchronous request blocks the browser (which would explain the GUI lock-up).
If this is not your problem, I think you want something like this task queue.
var queue = [];
queue.push(someTaskFunction);
queue.push(anotherTaskFunction);
// ...
var runQueue = (function () {
var len = queue.length, task = 0;
for (; task < len; task++) {
yield queue[task]();
}
}());
Call runQueue.next()
to execute the next task. Wrap it in a try..catch statement as such:
try {
runQueue.next();
} catch (e if (e instanceof StopIteration)) {}
本文标签:
版权声明:本文标题:settimeout - Is there a correct way to 'yield' in the cooperative threading sense in javascript? - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741654021a2390648.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论