HTML and CSS Reference
In-Depth Information
useless. In particular, you can't resume the worker; you'd have
to create a brand new one.
What you can do inside a worker
Within a Web Worker you don't have access to such plea-
sures as the DOM. In fact, if you need to do anything with the
DOM, you're going to have to prepare the work in the worker,
then pass it to the parent document to do the actual DOM
However there are a number of things you can do in a worker
(according to the specifi cation):
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 , etc.
Location object, the href of the worker script
Web Sockets (which we'll discuss in the final section of this
Web SQL Databases
Web Workers
The following code is all I need in my_worker.js to communicate
with the document from the earlier code listing:
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, the 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
NOTE Currently there are
no Web Worker implemen-
tations that support accessing
Web SQL Databases, though
there are fi xes in the works.
Search WWH ::

Custom Search