问题描述
我想知道当用户关闭产生工作线程的选项卡时会发生什么,而线程仍在工作。它会阻止一切吗?如果是这样,是否有办法在后台运行线程,即使该选项卡已关闭?解析方案
我想知道当用户关闭产生工作线程的选项卡时会发生什么,而线程仍在工作。它会阻止一切吗?如果是这样,是否有办法在后台运行线程,即使该选项卡已关闭?解析方案
MessagePort
传递给另一个窗口(即,消息端口的所有者不是 。 new SharedWorker(...)
),然后关闭旧窗口。但一个窗口必须始终保持打开状态。 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?
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.
这篇关于当标签在运行时关闭时,HTML5 Web工作线程会发生什么情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!