| title | Worker |
|---|---|
| slug | Web/API/Worker |
| page-type | web-api-interface |
| browser-compat | api.Worker |
{{APIRef("Web Workers API")}}{{AvailableInWorkers("window_and_worker_except_service")}}
The Worker interface of the Web Workers API represents a background task that can be created via script, which can send messages back to its creator.
Creating a worker is done by calling the Worker("path/to/worker/script") constructor.
Workers may themselves spawn new workers, as long as those workers are hosted at the same origin as the parent page.
Note that not all interfaces and functions are available to web workers. See Functions and classes available to Web Workers for details.
{{InheritanceDiagram}}
- {{domxref("Worker.Worker", "Worker()")}}
- : Creates a dedicated web worker that executes the script at the specified URL. This also works for Blob URLs.
Inherits properties from its parent, {{domxref("EventTarget")}}.
Inherits methods from its parent, {{domxref("EventTarget")}}.
- {{domxref("Worker.postMessage()")}}
- : Sends a message — consisting of any JavaScript object — to the worker's inner scope.
- {{domxref("Worker.terminate()")}}
- : Immediately terminates the worker. This does not let worker finish its operations; it is halted at once.
ServiceWorkerinstances do not support this method.
- : Immediately terminates the worker. This does not let worker finish its operations; it is halted at once.
error- : Fires when an error occurs in the worker.
message- : Fires when the worker's parent receives a message from that worker.
messageerror- : Fires when a
Workerobject receives a message that can't be deserialized.
- : Fires when a
The following code snippet creates a Worker object using the {{domxref("Worker.Worker", "Worker()")}} constructor, then uses the worker object:
const myWorker = new Worker("/worker.js");
const first = document.querySelector("input#number1");
const second = document.querySelector("input#number2");
first.onchange = () => {
myWorker.postMessage([first.value, second.value]);
console.log("Message posted to worker");
};For a full example, see our Basic dedicated worker example (run dedicated worker).
{{Specifications}}
Support varies for different types of workers. See each worker type's page for specifics.
{{Compat}}
In early versions of the spec, loading a cross-origin worker script threw a SecurityError. Nowadays, an {{domxref("Worker/error_event", "error")}} event is thrown instead.
- Using Web Workers
- Functions and classes available to Web Workers
- Other kind of workers: {{domxref("SharedWorker")}} and Service Worker.
OffscreenCanvasinterface