Worker

« DOM Reference

The Worker interface represents a background task that can be easily created and can send messages back to their creators. Creating a worker is as simple as calling the Worker() constructor, specifying a script to be run in the worker thread.

Of note is the fact that workers may in turn spawn new workers as long as those workers are hosted within the same origin as the parent page.  In addition, workers may use XMLHttpRequest for network I/O, with the exception that the responseXML and channel attributes on XMLHttpRequest always return null.

Not all interfaces and functions are available to the script associated with a Worker.

In Firefox, if you want to use workers in extensions and would like to have access to js-ctypes, you should use the ChromeWorker object instead.

Properties

Inherits properties from its parent, AbstractWorker.

Worker.onmessage
Is an EventListener that is called whenever a MessageEvent of type message bubbles through the worker. The message is stored in the event's data property.

Constructors

Worker()
Creates a dedicated web worker that executes the script at the specified URL.

Methods

Inherits methods from its parent, AbstractWorker.

Worker.postMessage()
Sends a message, that is any JavaScript object to the worker's inner scope.
Worker.terminate()
Immediately terminates the worker. This does not offer the worker an opportunity to finish its operations; it is simply stopped at once.

Specifications

Specification Status Comment
WHATWG HTML Living Standard Living Standard No change from Web Workers.
Web Workers Candidate Recommendation Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support (Yes) (Yes) 10.0 (Yes) (Yes)
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support (Yes) (Yes) 10.0 (Yes) (Yes)

See also

Document Tags and Contributors

Last updated by: soswow,