WorkerGlobalScope - Web API 接口参考 编辑
Web Workers API 的 WorkerGlobalScope
接口 是一个代表了任何 scope of worker的接口. Workers 没有浏览内容; 这个 scope 包含的信息总是通过 Window
objects传递 — 比如 event handlers, the console or the associated WorkerNavigator
object.每个 WorkerGlobalScope
都有自己的事件循环.
每个 worker type都有专门的这个接口: DedicatedWorkerGlobalScope
for dedicated workers, SharedWorkerGlobalScope
for shared workers, and ServiceWorkerGlobalScope
for ServiceWorker. self
属性返回每个内容的专门 scope .
Properties
This interface inherits properties from the EventTarget
interface and implements properties from WindowTimers
, WindowBase64
, and WindowEventHandlers
.
Standard properties
WorkerGlobalScope.caches
只读- Returns the
CacheStorage
object associated with the current context. This object enables service worker functionality such as storing assets for offline use, and generating custom responses to requests. WorkerGlobalScope.navigator
只读- Returns the
WorkerNavigator
associated with the worker. It is a specific navigator object, mostly a subset of theNavigator
for browsing scopes, but adapted to workers. WorkerGlobalScope.self
只读- Returns a reference to the
WorkerGlobalScope
itself. Most of the time it is a specific scope likeDedicatedWorkerGlobalScope
,SharedWorkerGlobalScope
orServiceWorkerGlobalScope
. WorkerGlobalScope.location
只读- Returns the
WorkerLocation
associated with the worker. It is a specific location object, mostly a subset of theLocation
for browsing scopes, but adapted to workers.
Non-standard properties
WorkerGlobalScope.performance
只读- Returns the
Performance
associated with the worker. It is a regular performance object, except that only a subset of its property and methods are available to workers. WorkerGlobalScope.console
只读- Returns the
Console
associated with the worker.
Event Handlers
This interface inherits event handlers from the EventTarget
interface and implements event handlers from WindowTimers
, and WindowBase64
.
WorkerGlobalScope.onerror
- Is an
EventHandler
representing the code to be called when theerror
event is raised. WorkerGlobalScope.onoffline
- Is an
EventHandler
representing the code to be called when theoffline
event is raised. WorkerGlobalScope.ononline
- Is an
EventHandler
representing the code to be called when theonline
event is raised. WorkerGlobalScope.onlanguagechange
- An
EventHandler
fired at the global/worker scope object when the user's preferred languages change.
WorkerGlobalScope.onclose
- Is an
EventHandler
representing the code to be called when theclose
event is raised. WorkerGlobalScope.onrejectionhandled
- An event handler for handled
Promise
rejection events. WorkerGlobalScope.onunhandledrejection
- An event handler for unhandled
Promise
rejection events.
Methods
This interface inherits methods from the EventTarget
interface and implements methods from WindowTimers
, WindowBase64
, WindowEventHandlers
, and GlobalFetch
.
Standard methods
WorkerGlobalScope.close()
- Discards any tasks queued in the
WorkerGlobalScope
's event loop, effectively closing this particular scope. WorkerGlobalScope.importScripts()
- Imports one or more scripts into the worker's scope. You can specify as many as you'd like, separated by commas. For example:
importScripts('foo.js', 'bar.js');
Non-standard methods
WorkerGlobalScope.dump()
- Allows you to write a message to stdout — i.e. in your terminal. This is the same as Firefox's
window.dump
, but for workers.
Methods implemented from elsewhere
WindowBase64.atob()
- Decodes a string of data which has been encoded using base-64 encoding.
WindowBase64.btoa()
- Creates a base-64 encoded ASCII string from a string of binary data.
WindowTimers.clearInterval()
- Cancels the repeated execution set using
WindowTimers.setInterval()
. WindowTimers.clearTimeout()
- Cancels the repeated execution set using
WindowTimers.setTimeout()
. ImageBitmapFactories.createImageBitmap()
- Accepts a variety of different image sources, and returns a
Promise
which resolves to anImageBitmap
. GlobalFetch.fetch()
- Starts the process of fetching a resource.
WindowTimers.setInterval()
- Schedules the execution of a function each X milliseconds.
WindowTimers.setTimeout()
- Sets a delay for executing a function.
Example
You won't access WorkerGlobalScope
directly in your code; however, its properties and methods are inherited by more specific global scopes such as DedicatedWorkerGlobalScope
and SharedWorkerGlobalScope
. For example, you could import another script into the worker and print out the contents of the worker scope's navigator
object using the following two lines:
importScripts('foo.js');
console.log(navigator);
Note: Since the global scope of the worker script is effectively the global scope of the worker you are running (DedicatedWorkerGlobalScope
or whatever) and all worker global scopes inherit methods, properties, etc. from WorkerGlobalScope
, you can run lines such as those above without specifying a parent object.
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard WorkerGlobalScope | Living Standard | No change from Unknown. |
Service Workers | Working Draft | Defines caches . |
Unknown WorkerGlobalScope | Unknown | Initial definition |
Browser compatibility
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 4 | 3.5 (1.9.1) | 10 | 10.6 | 4 |
ononline , onoffline | (Yes) | 29 (29) | ? | ? | ? |
console | (Yes) | 29 (29)[1] 30 (30) | ? | ? | ? |
performance | (Yes) | 34 (34) | ? | ? | ? |
caches | 40 | ? | 未实现 | ? | 未实现 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | ? | (Yes) | 1.0 (1.9.1) | 1.0.1 | 10 | 11.5 | 5.1 |
ononline , onoffline | ? | (Yes) | 29.0 (29)[1] 30.0 (30) | 1.4 | ? | ? | ? |
console | ? | (Yes) | 29.0 (29) | 1.4 | ? | ? | ? |
performance | ? | (Yes) | 34.0 (34) | 2.1 | ? | ? | ? |
caches | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
[1] Gecko 29 implemented this as WorkerConsole
. Since version 30 it uses the regular Console
.
See also
- Other global object interface:
Window
,DedicatedWorkerGlobalScope
,SharedWorkerGlobalScope
, ,ServiceWorkerGlobalScope
- Other Worker-related interfaces:
Worker
,WorkerLocation
,WorkerGlobalScope
, andServiceWorkerGlobalScope
. - Using web workers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论