One worker performs his task in the queue, that is, one task in time. Try the following example:
<!DOCTYPE html> <script> var worker = new Worker('worker.js'); worker.postMessage({ task: 1, iterations: 100 }); </script>
worker.js:
self.onmessage = function(event) { for (var i = 0; i < event.data.iterations * 1000 * 1000 * 10; i++) {}; self.postMessage("Finished task " + event.data.task); }
Ouput:
Finished task 1 Finished task 2
Tasks always end in order, that is, first slow and then fast. (If the task was completed in parallel, the second task would first finish with a clear margin.)
(Just to be clear: a postMessage call always blocks execution in its context (like any function call), but effectively returns βimmediatelyβ because posting the message itself is very fast. This is probably not what you requested.)
Note. Chrome throws a security exception if you try to download work.js from a local drive, it works in Safari and Firefox.
jholster
source share