HTML and CSS Reference
In-Depth Information
// perform some amazing feat of image processing
var imageData = amazingImageProcess(data.imageData,
¬ data.x, data.width);
postMessage(JSON.stringify({
imageData: imageData,
x: data.x
}));
// self close
close();
};
Notice also how photofilter.js , once it's done performing its
task, calls the close() method. This allows the worker to termi-
nate itself, since it's no longer needed.
Importing scripts and
libraries to your worker
The concept of Web Workers is very much about modularis-
ing a block of code or functionality and running it in a stand-
alone environment (that is, the worker itself). But Web Workers
can also load external JavaScript files and libraries via the
importScripts method.
This is one of the few worker-specific methods. It accepts a list
of URLs and loads them into the worker synchronously. You can
load one script at a time, or you can load multiple scripts from
within the worker:
importScripts('xhr.js');
importScripts('prime.js', 'number_crunch.js',
¬ 'captain_crunch.js');
Each script is processed one at a time. The script must be on the
same origin as the worker—the same domain, cname, and so on.
The worker then synchronously loads the JavaScript into itself,
returning to continue only once the script has finished processing.
Sharing a load with SharedWorkers
Another type of Web Worker is the SharedWorker , currently sup-
ported only in Chrome, Safari, and Opera. A shared worker is
pretty much like an average Web Worker except that multiple
documents can access the same instance of the worker. This
 
Search WWH ::




Custom Search