Функции и классы доступные веб-воркерам
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
(en-US) for dedicated workersSharedWorkerGlobalScope
(en-US) for shared workersServiceWorkerGlobalScope
(en-US) for service workers
Some of the functions that are common to all workers and to the main thread (from WindowOrWorkerGlobalScope
) are: atob()
, btoa()
, clearInterval()
(en-US), clearTimeout()
, dump()
(en-US)
Non-standard
, setInterval()
, setTimeout()
.
The following functions are only available to workers:
WorkerGlobalScope.importScripts()
(all workers),DedicatedWorkerGlobalScope.postMessage
(en-US) (dedicated workers only).
Web APIs available in workers
Note: 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
(en-US)Cache API
Channel Messaging API
(en-US)Console API
Crypto
CustomEvent
(en-US),DOMRequest
andDOMCursor
Fetch
FileReader
FileReaderSync
(en-US) (only works in workers!)FormData
ImageData
IndexedDB
- Network Information API
Notifications
Performance
PerformanceEntry
(en-US)PerformanceMeasure
(en-US)PerformanceMark
(en-US)PerformanceObserver
(en-US)PerformanceResourceTiming
(en-US)Promise
- Server-sent events
ServiceWorkerRegistration
TextEncoder
(en-US) andTextDecoder
(en-US)URL
- WebGL with
OffscreenCanvas
(en-US) WebSocket
XMLHttpRequest
(although theresponseXML
andchannel
attributes are always null).
Workers can also spawn other workers, so these APIs are also available: