运行者 Worker 接口是Web Workers API 的一部分,代表一个后台任务,它容易被创建并向创建者发回消息。创建一个运行者只要简单的调用Worker()构造函数,指定一个脚本,在工作线程中执行。

运行者能够依序产生新(嵌套)的运行者,只要这些运行者有同源(origin)的父页面(注意:Blink暂时不支持嵌套运行者)。 此外,运行者还能够使用 XMLHttpRequest 实现网络 I/O 操作, 只不过 XMLHttpRequest上的 responseXML 与 channel 两个属性值始终返回 null。

运行者 Workder 支持的函数 页面提供了一个 worker 支持的全局函数列表。

火狐浏览器中, 如果在扩展使用运行者,不应使用(js-ctypes),应该使用 ChromeWorker 对象。

构造函数

Worker()
创建一个专用Web worker,它只执行URL指定的脚本。Worker不指定URL时,而由使用Blob创建。

属性

继承父对象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 AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 4Edge Full support YesFirefox Full support 3.5IE Full support 10Opera Full support 10.6Safari Full support 4WebView Android Full support 4Chrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 11.5Safari iOS Full support 5.1Samsung Internet Android Full support Yes
Worker() constructorChrome Full support 4Edge Full support YesFirefox Full support 3.5IE Full support 10Opera Full support 10.6Safari Full support 4WebView Android Full support 4Chrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 11.5Safari iOS Full support 5.1Samsung Internet Android Full support Yes
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 18Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 11.5Safari iOS Full support 5.1Samsung Internet Android Full support Yes
onmessageerrorChrome Full support 60Edge ? Firefox Full support 57IE ? Opera Full support 47Safari ? WebView Android Full support 60Chrome Android Full support 60Edge Mobile ? Firefox Android Full support 57Opera Android Full support 47Safari iOS ? Samsung Internet Android No support No
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 YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support 47Safari 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 18Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 11.5Safari iOS Full support 5.1Samsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No 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.

 

相关链接

文档标签和贡献者

标签: 
最后编辑者: Syclover-HardToName,