JavaScripture
Contribute via GitHub Feedback

JavaScript WorkerGlobalScope : Global

The following are properties of the global object when running inside a Worker background process. They can be accessed from anywhere without additional qualifiers.

Instance Properties

location : Location  

Returns the location of the worker script.

Example:

Run

Results:

 

self : WorkerGlobalScope  

Returns the global object itself.

Example:

Run

Results:

 

Instance Methods

addEventListener 2 variants
addEventListener(type : String, listener : Function, [useCapture : Boolean]) : undefined
listener(event : Event) : undefined
addEventListener(type : String, listener : EventListener, [useCapture : Boolean]) : undefined
close() : undefined

Stops the worker.

Example:

Run

Results:

 

dispatchEvent(event : Event) : Boolean
postMessage(message : Object, [transfer : Array]) : undefined

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

Example:

Run

Results:

 

removeEventListener 2 variants
removeEventListener(type : String, listener : Function, [useCapture : Boolean]) : undefined
listener(event : Event) : undefined
removeEventListener(type : String, listener : EventListener, [useCapture : Boolean]) : undefined

Instance Events

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

Called when Worker.postMessage() is called. event.data contains the message passed to postMessage().

Example:

Run

Results:

 

onerror / 'error'  
listener(error : String) : undefined

Fired when there is an error in the worker.

Example:

Run

Results:

 

onoffline / 'offline'  
listener(event : Event) : undefined

Fired when the browser loses network connection.

ononline / 'online'  
listener(event : Event) : undefined

Fired when the browser gains network connection.