WorkerGlobalScope object
Represents the global scope of the worker.
Syntax
self
DOM Information
Inheritance Hierarchy
The WorkerGlobalScope does not inherit from any class or interface.Members
The WorkerGlobalScope object has these types of members:
Events
The WorkerGlobalScope object has these events.
| Event | Description |
|---|---|
| error |
Returns an error if a worker throws an unhandled exception. |
| message |
Returns a message from the parent thread to the worker thread, or between two MessagePort objects. |
Methods
The WorkerGlobalScope object has these methods.
| Method | Description |
|---|---|
| addEventListener |
Registers an event handler for the specified event type. |
| close |
Stops a worker when called inside worker code. |
| dispatchEvent |
Sends an event to the current element. |
| importScripts |
Loads and executes one or more scripts into the worker specified by a URL. |
| msWriteProfilerMark |
Writes a profiling event. |
| postMessage |
Sends a message from the worker to the parent thread. |
| removeEventListener |
Removes an event handler that the addEventListener method registered. |
Properties
The WorkerGlobalScope object has these properties.
| Property | Access type | Description |
|---|---|---|
|
Read-only |
Returns the windows console object to use to send messages. | |
|
Read-only |
Allows access to the Indexed Database API, as implemented by Internet Explorer 10. | |
|
Read-only |
Returns the WorkerLocation object of a worker. | |
|
Read-only |
Returns a WorkerNavigator object. | |
|
Read-only |
Contains a reference to the WorkerGlobalScope object. |
Remarks
The WorkerGlobalScope object is accessed by using the self or this property and is accessed only inside a worker. A worker has the following functionality within its scope:
- It can use the XMLHttpRequest method.
- It has a self property, which contains a reference to the worker object.
- It can call all global JavaScript core methods.
- It has a navigator object, which provides the appName, appVersion, onLine, userAgent, and platform properties but no others.
- The location property creates the WorkerLocation object, which contains URL information on the worker's location. This property is similar to the Window.location property, but is read-only.
- It supports the worker versions of the setTimeout, setInterval, clearTimeout, and clearInterval methods, which are similar to the methods of the same name for the Window object.
- It can load other JavaScript files into the scope of the worker using the importScripts method.
Workers do not have access to the Document Object Model (DOM), nor the window, document, and parent objects.
To terminate a worker thread from inside, use the close method. From the main document, use the terminate method to terminate the worker thread.
You can send messages to the main document using postMessage and receive messages from the main document using the onmessage message handler.
You can create new child workers from inside a worker. They must all run in the same host and their location depends on the parent worker of the new worker, not the original webpage. Because a new worker is likely to be in a separate process, you shouldn't create too many workers at once. Also, because messages between workers and their parents are copies and not shared, too many workers can use up resources quickly.