这篇翻译不完整。请帮忙从英语翻译这篇文章

Web Workers是一种机制,通过它可以使一个脚本操作在与Web应用程序的主执行线程分离的后台线程中运行。这样做的优点是可以在单独的线程中执行繁琐的处理,让主(通常是UI)线程运行而不被阻塞/减慢。

Web Workers 概念与用法

一个 worker 是使用构造函数创建的一个对象(例如,Worker()), 运行一个命名的 JavaScript文件 — 这个文件包含了将在 worker 线程中运行的代码,并且 worker 在与当前 window不同的另一个全局上下文中运行。这个上下文由专用worker的情况下的一个DedicatedWorkerGlobalScope 对象表示(标准 workers 由单个脚本使用; 共享workers使用SharedWorkerGlobalScope)。

你可以在你的 worker 线程中运行任意的代码,以下情况除外:你不能直接在 worker 线程中操纵 DOM 元素, 或者使用某些 window 对象中默认的方法和属性。 但是 window 对象中很多的方法和属性你是可以使用的,包括 WebSockets,以及诸如 IndexedDB 和 FireFox OS 中独有的 Data Store API 这一类数据存储机制。更多信息请参见: Functions and classes available to workers 。

主线程和 worker 线程之间通过这样的方式互相传输信息:两端都使用 postMessage() 方法来发送信息, 并且通过 onmessage 这个 event handler来接收信息。 (传递的信息包含在 Message 这个事件的数据属性内) 。数据的交互是通过传递副本,而不是直接共享数据。

一个 worker 可以生成另外的新的 worker,这些 worker 的宿主和它们父页面的宿主相同。  此外,worker 可以通过 XMLHttpRequest 来访问网络,只是 XMLHttpRequest 的 responseXML 和 channel 这两个属性将总是 null 。

除了专用 worker 之外,还有一些其他种类的 worker :

  • Shared workers are workers that can be utilized by multiple scripts running in different windows, IFrames, etc., as long as they are in the same domain as the worker. They are a little more complex than dedicated workers — scripts must communicate via an active port. See SharedWorker for more details.
  • ServiceWorkers essentially act as proxy servers that sit between web applications, and the browser and network (when available). They are intended to (amongst other things) enable the creation of effective offline experiences, intercepting network requests and taking appropriate action based on whether the network is available and updated assets reside on the server. They will also allow access to push notifications and background sync APIs.
  • Chrome Workers are a Firefox-only type of worker that you can use if you are developing add-ons and want to use workers in extensions and have access to js-ctypes in your worker. See ChromeWorker for more details. 
  • Audio Workers provide the ability for direct scripted audio processing to be done inside a web worker context.

Note: As per the Web workers Spec, worker error events should not bubble (see bug 1188141. This has been implemented in Firefox 42.

Web Worker 接口

AbstractWorker
Abstracts properties and methods common to all kind of workers (i.e. Worker or SharedWorker).
Worker
Represents a running worker thread, allowing you to pass messages to the running worker code.
SharedWorker
Represents a specific kind of worker that can be accessed from several browsing contexts, being several windows, iframes or even workers.
WorkerGlobalScope
Represents the generic scope of any worker (doing the same job as Window does for normal web content). Different types of worker have scope objects that inherit from this interface and add more specific features.
DedicatedWorkerGlobalScope
Represents the scope of a dedicated worker, inheriting from WorkerGlobalScope and adding some dedicated features.
SharedWorkerGlobalScope
Represents the scope of a shared worker, inheriting from WorkerGlobalScope and adding some dedicated features.
WorkerNavigator
Represents the identity and state of the user agent (the client):

示例

We have created a couple of simple demos to show basic usage:

You can find out more information on how these demos work in Using web workers.

规范

Specification Status Comment
WHATWG HTML Living Standard Living Standard No change from Web Workers.
Web Workers Editor's Draft Initial definition.

浏览器兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 4 Unknown (3.5) 10.0 10.6 4
Shared workers 4 29 (29) 未实现 10.6 4
Passing data using structured cloning 13 8 (8) 10.0 11.5 6
Passing data using  transferable objects 17 webkit
21
18 (18) 未实现 15 6
Global URL 10 as webkitURL
23
21 (21) 11 15 6 as webkitURL
Feature Android Chrome Mobile Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 4.4 4 3.5 1.0.1 10.0 11.5 5.1
Shared workers 4 29 1.4 未实现
Passing data using structured cloning 4 8 1.0.1 未实现
Passing data using  transferable objects 18 1.0.1 未实现

相关链接

文档标签和贡献者

 此页面的贡献者: xgqfrms-GitHub, peterwang1996, forclan, wth, chrisdavidmills
 最后编辑者: xgqfrms-GitHub,