Worker - Web APIs 编辑

The Worker interface of the Web Workers API represents a background task that can be created via script, which can send messages back to its creator. Creating a worker is done by calling the Worker("path/to/worker/script") constructor.

Workers may themselves spawn new workers, as long as those workers are hosted at the same origin as the parent page. (Note: nested workers are not yet implemented in WebKit).

Not all interfaces and functions are available to scripts inside a Worker. Workers may use XMLHttpRequest for network communication, but its responseXML and channel attributes are always null. (fetch is also available, with no such restrictions.)

Constructors

Worker()
Creates a dedicated web worker that executes the script at the specified URL. This also works for Blob URLs.

Properties

Inherits properties from its parent, EventTarget, and implements properties from AbstractWorker.

Event handlers

AbstractWorker.onerror
An EventListener called whenever an ErrorEvent of type error bubbles through to the worker. This is inherited from AbstractWorker.
Worker.onmessage
An EventListener called whenever a MessageEvent of type message bubbles through the worker — i.e. when a message is sent to the parent document from the worker via DedicatedWorkerGlobalScope.postMessage. The message is stored in the event's data property.
Worker.onmessageerror
Is an EventHandler representing the code to be called when the messageerror event is raised.

Methods

Inherits methods from its parent, EventTarget, and implements methods from AbstractWorker.

Worker.postMessage()
Sends a message — consisting of any JavaScript object — to the worker's inner scope.
Worker.terminate()
Immediately terminates the worker. This does not let worker finish its operations; it is halted at once. ServiceWorker instances do not support this method.

Events

message
Fires when the worker's parent receives a message from that worker.
Also available via the onmessage property.
messageerror
Fires when a Worker object receives a message that can't be deserialized.
Also available via the onmessageerror property.
rejectionhandled
Fires every time a Promise rejects, regardless of whether or not there is a handler to catch the rejection.
Also available through the onrejectionhandled event handler property.
unhandledrejection
Fires when a Promise rejects with no handler to catch the rejection.
Also available using the onunhandledrejection event handler property.

Example

The following code snippet creates a Worker object using the Worker() constructor, then uses the worker object:

var myWorker = new Worker('/worker.js');
var first = document.querySelector('input#number1');
var second = document.querySelector('input#number2');

first.onchange = function() {
  myWorker.postMessage([first.value, second.value]);
  console.log('Message posted to worker');
}

For a full example, see ourBasic dedicated worker example (run dedicated worker).

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of 'Worker' in that specification.
Living Standard

Browser compatibility

BCD tables only load in the browser

Support varies for different types of workers. See each worker type's page for specifics.

Cross-origin worker error behaviour

In early versions of the spec, loading a cross-origin worker script threw a SecurityError. Nowadays, an error event is thrown instead. Find out how to deal with this in Loading cross-origin worker now fires error event instead of throwing; worker in sandboxed iframe no longer allowed.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:114 次

字数:9301

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文