Search

7/15/2009

web workers

html 5 web workers


var worker = new Worker('worker.js');
worker.onmessage = function (event) {
document.getElementById('result').textContent = event.data;
};


var n = 1;
search: while (true) {
n += 1;
for (var i = 2; i <= Math.sqrt(n); i += 1)
if (n % i == 0)
continue search;
// found a prime!
postMessage(n);
}

Using web workers - MDC
Workers provide a simple means for web content to run scripts in background threads. Once created, a worker can send messages to the spawning task by posting messages to an event handler specified by the creator.

http://www.0xdeadbeef.com/weblog/?p=1050
Worker Threads
* Spawn OS-threads to run JavaScript
* Avoids window freezing
* Simple communication methods with main thread
– postMessage(), onmessage and onerror
* Has XMLHttpRequest Access, timeouts


tag: javascript thread

沒有留言: