What happens to an HTML5 web worker thread when th

2019-01-22 20:31发布

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?

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-22 21:24

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.

查看更多
在下西门庆
3楼-- · 2019-01-22 21:29

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.

Whenever a Document object is discarded, it must be removed from the list of the worker's Documents of each worker whose list contains that Document.

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.

查看更多
登录 后发表回答