Search code examples
multithreadinghtmlweb-worker

Is HTML5 Web Workers for a page or across pages


I went through couple of articles that talk about Web Workers in HTML5 and I was not able to clearly understand the following:

  • Is the life cycle for a web worker limited to a single page or across pages of an domain?
  • Is the life cycle for a web worker persistant after a page is loaded?
  • Are web workers a better option to using than an ajax call within setInterval?

Appreciate if someone could help me shed some light on the above questions.


Solution

    • Is the life cycle for a web worker limited to a single page or across pages of an domain?

    Dedicated worker is limited to a single page, while shared worker can be related to many webpages

    • Is the life cycle for a web worker persistant after a page is loaded?

    It could be persistent if it has onmessage() defined, and it could also returns after execution, if I understand correctly. In spec it says: let that run until it either returns, fails to catch an exception, or gets prematurely aborted by the "kill a worker" or "terminate a worker"...

    • Are web workers a better option to using than an ajax call within setInterval?

    It depends. You can't simply say it is better or not. At the moment Web worker is mainly used for offloading complex JavaScript algorithms to run in the background.