What's the difference between Shared Worker and Worker in HTML5?

后端 未结 5 449
清歌不尽
清歌不尽 2020-12-07 20:20

After reading this blog post: http://www.sitepoint.com/javascript-shared-web-workers-html5/

I don\'t get it. What\'s the difference between a Worker and

5条回答
  •  失恋的感觉
    2020-12-07 20:33

    Shared worker allow all front page context script, which call constructor: new SharedWorker("path-to-shared-worker-file.js") to share the same instance of shared worker file running in the background context(another thread of running javascript in the back).

    for example, when webpage #1 calling that constructor, if found there is no shared worker loaded up in the back yet, it will cause the background context to download the file and load it up, then later when webpage #2 calling that same constructor(same file path), it found there is an existing shared worker running, it will just using the same one. When worker.port.start() function invoked, it will cause shared worker file onconnect event handler invoked to register caller and obtain the handle to communicate the client port(for postMessage back for example).

    However worker, every webpage above will load up one worker file in the background for every single front page, which is not shared the same worker.js instance.

提交回复
热议问题