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, EventTarget, and implements properties from AbstractWorker.
-
AbstractWorker.onerror -
Is an
EventListenerthat is called whenever anErrorEventof typeerrorbubbles through the worker. -
Worker.onmessage -
Is an
EventListenerthat is called whenever aMessageEventof typemessagebubbles through the worker. The message is stored in the event'sdataproperty.
Constructors
-
Worker() - Creates a dedicated web worker that executes the script at the specified URL.
Methods
Inherits methods from its parent, EventTarget, and implements properties from AbstractWorker.
-
Worker.postMessage() -
Sends a message, that is
anyJavaScript 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 The definition of 'Worker' in that specification. |
Living Standard | No change from Web Workers. |
| Web Workers The definition of 'Worker' in that specification. |
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
- Using web workers
- Functions available to workers
- Other kind of workers:
SharedWorkerandServiceWorker - Non-standard, Gecko-specific workers:
ChromeWorker, used by extensions.