JavaScripture
Contribute via GitHub Feedback

Worker : EventTarget

Worker allows a page to run Javascript on a background thread so the main UI thread can remain responsive. Workers do not have access to the DOM or any global variables in the main UI thread and must use the postMessage() method to communicate with the main thread. See WorkerGlobalScope for the global properties and methods available inside the worker.

Constructors

new Worker(scriptURL : String) : Worker

Creates and starts a background thread that runs the code pointed at by scriptURL.

Example:

Run

Results:

 

Instance Methods

postMessage(message : Object, [transfer : Array]) : undefined

Sends a message to the worker. The worker receives the message through the onmessage event. message will be in the event.data property.

Example:

Run

Results:

 

terminate() : undefined

Stops the worker.

Example:

Run

Results:

 

Instance Events

onerror / 'error'  
listener(event : ErrorEvent) : undefined

Fires when an error occurs in the worker.

Example:

Run

Results:

 

onmessage / 'message'  
listener(event : MessageEvent) : undefined

Fires when the worker calls postMessage(). The message passed to postMessage is in event.data.

Example:

Run

Results: