Worker
接口在
Web 工作者 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")
构造函数。
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.)
Worker()
继承的特性来自其父级,
EventTarget
,和实现的特性来自
AbstractWorker
.
AbstractWorker.onerror
EventListener
called whenever an
ErrorEvent
类型
error
bubbles through to the worker. This is inherited from
AbstractWorker
.
Worker.onmessage
EventListener
called whenever a
MessageEvent
类型
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
特性。
Worker.onmessageerror
EventHandler
表示要调用的代码当
messageerror
事件被引发。
继承方法来自其父级
EventTarget
, and implements methods from
AbstractWorker
.
Worker.postMessage()
Sends a message — consisting of any JavaScript object — to the worker's inner scope.
Worker.terminate()
ServiceWorker
instances do not support this method.
message
onmessage
特性。
messageerror
Worker
object receives a message that can't be
deserialized
.
onmessageerror
特性。
rejectionhandled
Promise
rejects, regardless of whether or not there is a handler to catch the rejection.
onrejectionhandled
event handler property.
unhandledrejection
Promise
rejects with no handler to catch the rejection.
onunhandledrejection
event handler property.
The following code snippet creates a
Worker
对象使用
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 our Basic dedicated worker example ( run dedicated worker ).
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
在该规范中的 Worker 定义。 |
实时标准 |
Support varies for different types of workers. See each worker type's page for specifics.
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Worker
|
Chrome 4 | Edge 12 | Firefox 3.5 | IE 10 | Opera 10.6 | Safari 4 | WebView Android 4 | Chrome Android 18 | Firefox Android 4 | Opera Android 11 | Safari iOS 5.1 | Samsung Internet Android 1.0 |
Worker()
构造函数
|
Chrome 4 | Edge 12 | Firefox 3.5 | IE 10 | Opera 10.6 | Safari 4 | WebView Android 4 | Chrome Android 18 | Firefox Android 4 | Opera Android 11 | Safari iOS 5.1 | Samsung Internet Android 1.0 |
message
event
|
Chrome 4 | Edge 12 | Firefox 3.5 | IE 10 | Opera 10.6 | Safari 4 | WebView Android 4 | Chrome Android 18 | Firefox Android 4 | Opera Android 11.5 | Safari iOS 5.1 | Samsung Internet Android 1.0 |
messageerror
event
|
Chrome 60 | Edge 18 | Firefox 57 | IE ? | Opera 47 | Safari ? | WebView Android 60 | Chrome Android 60 | Firefox Android 57 | Opera Android 47 | Safari iOS ? | Samsung Internet Android 8.0 |
onmessage
|
Chrome 4 | Edge 12 | Firefox 3.5 | IE 10 | Opera 10.6 | Safari 4 | WebView Android 4 | Chrome Android 18 | Firefox Android 4 | Opera Android 11 | Safari iOS 5.1 | Samsung Internet Android 1.0 |
onmessageerror
|
Chrome 60 | Edge 18 | Firefox 57 | IE ? | Opera 47 | Safari ? | WebView Android 60 | Chrome Android 60 | Firefox Android 57 | Opera Android 44 | Safari iOS ? | Samsung Internet Android 8.0 |
postMessage
|
Chrome Yes | Edge 12 | Firefox Yes |
IE
10
|
Opera 47 | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android Yes | Opera Android 44 | Safari iOS Yes | Samsung Internet Android Yes |
terminate
|
Chrome 4 | Edge 12 | Firefox 3.5 | IE 10 | Opera 10.6 | Safari 4 | WebView Android 4 | Chrome Android 18 | Firefox Android 4 | Opera Android 11 | Safari iOS 5.1 | Samsung Internet Android 1.0 |
完整支持
兼容性未知
见实现注意事项。
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
.
SharedWorker
and
Service Worker
.
Worker