当标签在运行时关闭时

当标签在运行时关闭时

本文介绍了当标签在运行时关闭时,HTML5 Web工作线程会发生什么情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道当用户关闭产生工作线程的选项卡时会发生什么,而线程仍在工作。它会阻止一切吗?如果是这样,是否有办法在后台运行线程,即使该选项卡已关闭?解析方案


是的,它停止了一切,(专用)工人不能活过它的主人。如果您使用可以拥有多个所有者的共享工作程序,只要至少有一个所有者还活着,该工作人员将只能保持活动状态。即使您将纠缠的 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工作线程会发生什么情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 06:29