I'm wondering what happens when a user closes the tab which spawned the worker thread, while the thread is still working. Does it halt everything? If so, is there a way to run the thread in the background even when the tab is closed?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Yes it halts everything, a (dedicated) worker can't outlive its owner. If you use a shared worker, which can have multiple owners, the worker will only stay alive as long as at least one owner is alive. This is the case even if you pass on the entangled
MessagePort
to another window (i.e. owner of the message port is not the owner of the worker).So, with shared workers you can "transfer" the ownership by opening a new window that establishes its own connection with the worker (with
new SharedWorker(...)
) and then close the old window. But one window must always remain open.Take a look here
http://www.whatwg.org/specs/web-apps/current-work/multipage/workers.html#workerglobalscope
I think it is confirming that once the browser goes away, any workers must stop.
In the case where you have one window using web workers, and you close that window (or tab), the worker goes away.
If you have a case where you have a window, that opens other windows or tabs, the workers can continue. But if you close everything, they all go away.