Worker

« Gecko DOM Reference

Workers are background tasks 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.

For a list of global functions available to workers, see Functions available to workers.

Gecko 2.0 note
(Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)

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

See Using web workers for examples and details.

Method overview

Worker (in DOMString scriptURL)

void postMessage(in JSObject message);

void terminate();

Properties

Property Type Description
onmessage EventListener An event listener that is called whenever a MessageEvent with type message bubbles through the worker. The message is stored in the event's data member.
onerror EventListener An event listener that is called whenever an ErrorEvent with type error bubbles through the worker.

The error message

The error message contains three properties:

message
A human-readable error message.
filename
The name of the script file in which the error occurred.
lineno
The line number of the script file on which the error occurred.

Constructor

Worker()

The constructor creates a web worker that executes the script at the specified URL. This script must obey the same-origin policy. Note that there is a disagreement among browser manufacturers about whether a data URI is of the same origin or not. Though Gecko 10.0 (Firefox 10.0 / Thunderbird 10.0) and later accept data URIs, that's not the case in all other browsers.

Worker(
  in DOMString aStringURL
);
Parameters
aStringURL
The URL of the script the worker is to execute. It must obey the same-origin policy (or be a data URI for Gecko 10.0 or later).
Return value

A new Worker.

Methods

postMessage()

Sends a message to the worker's inner scope. This accepts a single parameter, which is the data to send to the worker. The data may be any value or JavaScript object that does not contain functions or cyclical references (since the object is converted to JSON internally).

void postMessage(
  in JSObject aMessage
);
Parameters
aMessage
The object to deliver to the worker; this will be in the data field in the event delivered to the onmessage handler. This may be any value or JavaScript object that does not contain functions or cyclical references (since the object is converted to JSON internally).

terminate()

Immediately terminates the worker. This does not offer the worker an opportunity to finish its operations; it is simply stopped at once.

void terminate();
Parameters

None.


Tags (4)

Edit tags

Attachments (0)

 

Attach file