HTML and CSS Reference
In-Depth Information
However, there are a number of things you can do in a worker
(according to the specification):
postMessage and listen for inbound messages via onmessage
close , to end the current worker
Set event listeners
XMLHttpRequest , for Ajax requests
Timers, such as setTimeout , setInterval , and their clearing
All the core JavaScript functions: eval , isNaN , escape , and so on.
Location object, the href of the worker script
WebSockets (which we'll discuss in the next chapter)
EventSource (also in the next chapter)
Web SQL Databases (only implemented in Safari and Chrome)
Web Workers
The following code is all I need in my_worker.js to communicate
with the document from the earlier code listing:
this.onmessage = function (event) {
if ( == “hello worker!”) {
postMessage(“hello there, right back at you”);
} else {
postMessage(“Can't you see I'm busy, leave me alone”);
It's useful to know that, in a normal document, this keyword
would refer to the global scope, the window object. Here in the
worker, the global scope is the worker instance. It also means
that the this keyword inside of setTimeout and setInterval
is the worker instance (where this would otherwise be the
window  object).
In these examples so far, our worker hasn't done anything par-
ticularly special. How about a worker that searches for prime
numbers? This requires a super tight loop in JavaScript con-
stantly spinning around looking for values that match a prime.
All this and at the same time allowing your visitor to draw on a
NoTE Currently there are
no Web Worker implemen-
tations that support accessing
IndexedDB, though there are
fixes in the works. In the first
edition of this topic, it was Web
SQL Databases that weren't
supported; since that's changed,
and the IndexedDB spec is still
fairly new, I'd expect support to
come fairly quickly once
IndexedDB implementations
have settled down.
Search WWH ::

Custom Search