Worker

Worker 接口是 Web Workers API 的一部分,指的是一种可由脚本创建的后台任务,任务执行中可以向其创建者收发信息。要创建一个 Worker只须调用 Worker(URL) 构造函数,函数参数 `URL` 为指定的脚本。

Worker 也可以创建新的 Worker,当然,所有 Worker 必须与其创建者同源(注意:Blink暂时不支持嵌套 Worker)。 

需要注意的是,不是所有函数和构造函数(或者说…类)都可以在 Worker 中使用。具体参考页面 Worker 所支持的函数和类。Worker 可以使用 XMLHttpRequest 发送请求,但是请求的  responseXML 与 channel 两个属性值始终返回 null (fetch 仍可正常使用,没有类似的限制)。 

 如果你要在火狐浏览器的扩展使用 Worker 访问 js-ctypes,应使用 ChromeWorker 对象来替代。(译者注:这里没有看懂,希望有人能驳正,或添加说明)

构造函数

Worker()
创建一个专用Web worker,它只执行URL指定的脚本。使用 Blob URL 作为参数亦可。

属性

继承父对象EventTarget 的属性,以及实现对象 AbstractWorker的属性。

事件句柄

AbstractWorker.onerror
ErrorEvent 类型的事件冒泡到 worker 时,事件监听函数 EventListener 被调用. 它继承于 AbstractWorker.
Worker.onmessage
MessageEvent类型的事件冒泡到 worker 时,事件监听函数 EventListener 被调用.  例如,一个消息通过 DedicatedWorkerGlobalScope.postMessage,从执行者发送到父页面对象,消息保存在事件对象的 data 属性中.
Worker.onmessageerror
messageerror 类型的事件发生时,对应的EventHandler 代码被调用。

方法

继承父对象EventTarget 的方法,以及实现对象 AbstractWorker的方法。

Worker.postMessage()
发送一条消息到最近的外层对象,消息可由任何 JavaScript 对象组成。
Worker.terminate()
立即终止 worker。该方法不会给 worker 留下任何完成操作的机会;就是简单的立即停止。Service Woker 不支持这个方法。

示例

下面的代码通过构造函数 Worker()  创建了一个 Worker 对象。

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

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

完整的示例,请查阅 Basic dedicated worker example (run dedicated worker).

规范

规范 状态 说明
HTML Living Standard
Worker
Living Standard

浏览器兼容性

不同类型的worker兼容度不一致,详细参考具体定义的页面。

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
WorkerChrome Full support 4Edge Full support 12Firefox Full support 3.5IE Full support 10Opera Full support 10.6Safari Full support 4WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 5.1Samsung Internet Android Full support 1.0
Worker() constructorChrome Full support 4Edge Full support 12Firefox Full support 3.5IE Full support 10Opera Full support 10.6Safari Full support 4WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 5.1Samsung Internet Android Full support 1.0
message eventChrome Full support 4Edge Full support 12Firefox Full support 3.5IE Full support 10Opera Full support 10.6Safari Full support 4WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11.5Safari iOS Full support 5.1Samsung Internet Android Full support 1.0
messageerror eventChrome Full support 60Edge Full support 18Firefox Full support 57IE ? Opera Full support 47Safari ? WebView Android Full support 60Chrome Android Full support 60Firefox Android Full support 57Opera Android Full support 47Safari iOS ? Samsung Internet Android Full support 8.0
onmessageChrome Full support 4Edge Full support 12Firefox Full support 3.5IE Full support 10Opera Full support 10.6Safari Full support 4WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 5.1Samsung Internet Android Full support 1.0
onmessageerrorChrome Full support 60Edge Full support 18Firefox Full support 57IE ? Opera Full support 47Safari ? WebView Android Full support 60Chrome Android Full support 60Firefox Android Full support 57Opera Android Full support 44Safari iOS ? Samsung Internet Android Full support 8.0
postMessageChrome Full support YesEdge Full support 12Firefox Full support YesIE Full support 10
Notes
Full support 10
Notes
Notes Internet Explorer does not support Transferable objects.
Opera Full support 47Safari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support 44Safari iOS Full support YesSamsung Internet Android Full support Yes
terminateChrome Full support 4Edge Full support 12Firefox Full support 3.5IE Full support 10Opera Full support 10.6Safari Full support 4WebView Android Full support 4Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 11Safari iOS Full support 5.1Samsung Internet Android Full support 1.0

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.

跨域行为的错误事件

浏览器的早期版本中,加载跨域的执行者脚本导致 SecurityError事件。根据规范的变更,而新版本的浏览器只有error事件发生。关于如何处理这种事件的更多信息参考 Loading cross-origin worker now fires error event instead of throwing; worker in sandboxed iframe no longer allowed.

相关链接