What happens to an HTML5 web worker thread when the tab is closed while it's running?

前端 未结 2 849
失恋的感觉
失恋的感觉 2020-12-05 15:04

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

2条回答
  •  死守一世寂寞
    2020-12-05 15:19

    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.

提交回复
热议问题