Thread API
Object: Worker
The Worker object represents a thread to be run in the background.
Web workers work as follows:
- On the parent web page, a script runs in the main thread of the Window object.
- The script in the main thread spawns a new thread by constructing a new Worker object. The constructor takes as an argument the JavaScript file that it is to load and execute.
- The worker executes the script.
To create a new worker thread, you need only construct a new worker object with the JavaScript file it is to load as the argument. For example:
var worker = new Worker(workerScript.js)
Worker event handlers
Worker methods
|
Method name |
Description |
Support level |
Availability |
|---|---|---|---|
|
postMessage() |
Sends a message to the worker thread. The message contains the data to be passed between threads. |
6.0 or later |
|
|
terminate() |
Terminates the worker. |
Web Workers |
6.0 or later |
Next topic: Touch API
Previous topic: StorageEvent methods