Functions and classes available to Web Workers - Web APIs 编辑
In addition to the standard JavaScript set of functions (such as String
, Array
, Object
, JSON
, etc), there are a variety of functions available from the DOM to workers. This article provides a list of those.
Worker Contexts & Functions
Workers run in a different global context than the current window! While Window
is not directly available to workers, many of the same methods are defined in a shared mixin (WindowOrWorkerGlobalScope
), and made available to workers through their own WorkerGlobalScope
-derived contexts:
DedicatedWorkerGlobalScope
for dedicated workersSharedWorkerGlobalScope
for shared workersServiceWorkerGlobalScope
for service workers
Some of the functions that are common to all workers and to the main thread (from WindowOrWorkerGlobalScope
) are: atob()
, btoa()
, clearInterval()
, clearTimeout()
,dump()
, setInterval()
, setTimeout()
.
The following functions are only available to workers:
WorkerGlobalScope.importScripts()
(all workers),close()
(dedicated and shared workers only),DedicatedWorkerGlobalScope.postMessage
(dedicated workers and chrome workers only).
Web APIs available in workers
Note that if a listed API is supported by a platform in a particular version, then it can generally be assumed to work in web workers.
The following Web APIs are available to workers: Broadcast Channel API
, Cache API
,Channel Messaging API
,Console API
, Crypto
, CustomEvent
, Data Store
(Firefox only), DOMRequest
and DOMCursor
, Fetch
, FileReader
, FileReaderSync
(only works in workers!), FormData
, ImageData
, IndexedDB
, Network Information API, Notifications
, Performance
, PerformanceEntry
, PerformanceMeasure
, PerformanceMark
, PerformanceObserver
, PerformanceResourceTiming
, Promise
, Server-sent events, ServiceWorkerRegistration
, TextEncoder
and TextDecoder
, URL
, WebGL with OffscreenCanvas
(enabled behind a feature preference setting gfx.offscreencanvas.enabled
), WebSocket
, XMLHttpRequest
.
Workers can also spawn other workers, so these APIs are also available: Worker
, WorkerGlobalScope
, WorkerLocation
, WorkerNavigator
.
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论