Do web workers terminate when the user navigates to a new page (within the same application)

StackOverflow https://stackoverflow.com/questions/3955799

  •  08-10-2019
  •  | 
  •  

سؤال

For example, can i offload a task and allow the user to keep surfing my site whilst the javascript runs?

It seems if I navigate away from the page, the worker terminates.

هل كانت مفيدة؟

المحلول

A "page navigation" in the browser means that everything that used to exist (HTML, CSS, JavaScript, plugins, etc.) cease to exist, and the new page is loaded as if you had opened a new browser window. Web workers make more sense in the context of a "web application" than a "web page". If you are writing an application that lives in the browser, consider ditching the "page" metaphor and construct a UI that doesn't need to navigate the browser every time you change context.

نصائح أخرى

I haven't personally tried but... There are so called Shared Web Workers. Supposedly they can be accessed from any script of the same origin. As I understand, that means it can be accessed from multiple browser windows. I guess it shouldn't be terminated if you just navigate around on the same page. Sorry if I am wrong but maybe that's exactly what you need ;)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top