javascript worker between page loads

Viewed 85

I have the following code

w = new Worker('<?php echo base_url(); ?>'+"assets/js/web_worker.js");
w.postMessage({base_url:BASE_URL,site_url:SITE_URL});

This runs on each page load and the worker could take awhile to run task.

  1. Will the worker continue to run if I navigate away from page after I start it?
  2. It seems the worker will start each page load regardless if it is finished. How do a see if the worker is running and NOT start another worker?
1 Answers
  1. Will the worker continue to run if I navigate away from page after I start it?

No, as soon as the context from which it has been generated has been killed, the Worker gets killed too.

  1. It seems the worker will start each page load regardless if it is finished. How do a see if the worker is running and NOT start another worker?

Indeed, a new Worker will start every time, but there won't be an other worker running as per one.


That is, if we talk about dedicated Workers.

A Shared-Worker would survive a page reload if an other page keeps it active during the reload.

And while that would be a misuse, a Service-Worker would definitely survive.

Actually the best might have been to start a Shared-Worker from a Service-Worker, but I think this is still not doable, despite active discussions.

Related