HTML and CSS Reference
In-Depth Information
However, a worker does have several helpful things available to it. For example, it can
access the navigator object, to identify the user agent (browser) running it, and it can
load scripts into itself using the importScripts(...) command:
if (navigator.userAgent.test(/MSIE/)) { // UA sniffing is *bad* practice!!
self.onmessage = function(evt) {
/* ... */
loadScripts(...) loads one or more scripts in parallel, but always exe-
cutes them in the order requested. Also, loadScripts(...) executes syn-
chronously, meaning it blocks the rest of the worker until it finishes
loading and executing the scripts.
A worker may spawn another worker, as we've just seen. The code that created a worker
may also terminate it, by calling terminate() on the worker instance.
Finally, workers may use timeouts and intervals, including setTimeout(...) , clearTi
meout(...) , setInterval(...) , and clearInterval(...) . This would be useful if, for
instance, you wanted to have a worker running in the background every so often, no-
tifying the page each time it runs:
self.onmessage = function(evt) {
setInterval (function(){
self.postMessage(Math.random()); // send a random number back
}, 60*60*1000); // execute once per hour
See Also
The W3C specification for Web Workers at .
10.5 Web Sockets
You want to create persistent, two-way communication between your web application
and the server, so that both the browser and the server can send and receive data to
and from each other as needed.
Most browsers now have the native ability to establish a bidirectional socket connection
between themselves and the server, using the WebSocket API. This means that both sides
(browser and server) can send and receive data. Common use cases for Web Sockets
are live online games, stock tickers, chat clients, etc.
Search WWH ::

Custom Search