Worker

Esta tradução está incompleta. Ajude atraduzir este artigo.

A interface Worker da API do Web Worker representa uma tarefa em background que pode ser facilmente criada e pode enviar mensagens de volta ao seu criador. Criar um worker é possível chamando o construtor Worker() e especificando um script para ser executado em sua thread.

Um worker pode, por sua vez, gerar outros workers, contanto que estes estejam hospedados na mesma origem da página principal (Nota: workers aninhados não estão atualmente implementados no Blink). Além disso, workers podem utilizar XMLHttpRequest para E/S de rede, desde que seja estipulado que os atributos responseXML e channel em XMLHttpRequest retornem sempre null.

Nem todas as interfaces e funções estão disponíveis para o script associado a um Worker.

No Firefox, se você deseja utilizar workers em extensões e gostaria de ter acesso a js-ctypes, você deveria utilizar o objeto ChromeWorker .

Properties

Inherits properties from its parent, EventTarget, and implements properties from AbstractWorker.

Event handlers

AbstractWorker.onerror
An EventListener called whenever an ErrorEvent of type error bubbles through to the worker. This is inherited from AbstractWorker.
Worker.onmessage
An EventListener called whenever a MessageEvent of type message bubbles through the worker — i.e. when a message is sent to the parent document from the worker via DedicatedWorkerGlobalScope.postMessage. 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. Workers can also be constructed using Blobs.

Methods

Inherits methods from its parent, EventTarget, and implements properties from AbstractWorker.

Worker.postMessage()
Sends a message — which can consist of 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. ServiceWorker instances do not support this method.

Example

The following code snippet shows creation of a Worker object using the Worker() constructor and usage of the object:

var myWorker = new Worker("worker.js");
var first = document.querySelector('#number1');

first.onchange = function() {
  myWorker.postMessage([first.value,second.value]);
  console.log('Message posted to worker');
}

For a full example, see ourBasic dedicated worker example (run dedicated worker).

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.
Editor's Draft Initial definition.

Browser compatibility

Support varies for different types of workers. See each worker type's page for specifics.

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 4 3.5 10.0 10.6 4
Feature Android Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support 4.4 3.5 1.0.1 10.0 11.5 5.1 ?

See also

Etiquetas do documento e colaboradores

 Colaboradores desta página: gxovano
 Última atualização por: gxovano,