Is there a proper way to “succumb” in a cooperative streaming sense in javascript?

I am writing an ubiquity plugin, a long function callback for an ajax request blocks the GUI thread, causing firefox to block.

The obvious solution is to use some delayed execution (i.e. we want to periodically add a function to execute this function at the end of the event queue, and then allow other commands to execute.

The only way I can think of this is to use setimeout with a timeout of zero ... is it guaranteed to work or is there a better way to do this.

+6
javascript reactor settimeout ubiquity
source share
2 answers

Using setTimeout with a very small timeout ( 0 or very close to zero if you feel paranoid) is the only way to do this in a browser context. It works very well and is very reliable, but do not forget to get it often enough, but not too often, since it takes some time to get back to you ("some time" in the computer sense, of course, it is almost instantaneous [modulo other things, which you can do] in human terms).

+7
source share

Make sure you use an asynchronous request, since a synchronous request blocks the browser (which explains the GUI blocking).

If this is not your problem, I think you need 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 complete the following task. Wrap it in a try..catch file as such:

 try { runQueue.next(); } catch (e if (e instanceof StopIteration)) {} 
+3
source share