Sharing variables between web workers? [global variables?]

前端 未结 4 431
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-17 19:06

Is there any way for me to share a variable between two web workers? (Web workers are basically threads in Javascript)

In languages like c# you have:



        
4条回答
  •  花落未央
    2021-01-17 19:21

    There are two options to share data between dedicated workers:

    1. Shared Workers

    The SharedWorker interface represents a specific kind of worker that can be accessed from several browsing contexts, such as several windows, iframes or even workers.

    Spawning a Shared Worker in a Dedicated Worker

    2. Channel Messaging API

    The Channel Messaging API allows two separate scripts running in different browsing contexts attached to the same document (e.g., two IFrames, or the main document and an IFrame, two documents via a SharedWorker, or two workers) to communicate directly, passing messages between one another through two-way channels (or pipes) with a port at each end.

    How to call shared worker from the web worker?

提交回复
热议问题