简体   繁体   English

如何从内部停止 js web 工作人员?

[英]How to stop js web worker from within?

TLDR: What is an alternative to WorkerGlobalScope.close() which is not deprecated TLDR:什么是WorkerGlobalScope.close()的替代品,它不被弃用

I want to close/stop/terminate a Javascript web worker from the inside, after its job is done, since there is a setInterval() which keeps being active even after the main work is done.我想在完成工作后从内部关闭/停止/终止 Javascript web 工作人员,因为有一个setInterval()即使在主要工作完成后仍保持活动状态。

Reading this SO question ( JavaScript Web Worker - close() vs terminate() ), it seems like self.close() is what I'm searching for.阅读这个 SO 问题( JavaScript Web Worker - close() vs terminate() ),似乎self.close()是我正在寻找的。 But apparently this is deprecated (stated here ) without any mention of alternatives.但显然,这已被弃用( 在此处说明),没有提及替代方案。

The only alternative I have come up with so far would be to send a message to the main thread which then invokes a worker.terminate() call, but that seems like overkill since I would then need to check every message that comes this way for a termination request.到目前为止,我想出的唯一选择是将消息发送到主线程,然后调用worker.terminate()调用,但这似乎有点过头了,因为我需要检查以这种方式出现的每条消息终止请求。

Am I missing something?我错过了什么吗? What should I use?我应该使用什么?

This notice is misleading .close() is not deprecated at all.此通知具有误导性.close()根本不被弃用。

What happens here is that in the specs WorkerGlobalScope is an interface that is used by different worker types (DedicatedWorkers, ServiceWorkers, SharedWorkers, and maybe other Worklets in other specs).这里发生的情况是,在规范中, WorkerGlobalScope是一个由不同工作类型(DedicatedWorkers、ServiceWorkers、SharedWorkers 以及其他规范中的其他 Worklet)使用的接口。
All these types don't have a .close method (eg ServiceWorkers don't), and thus this method has been moved to each specific types of worker global scope's definition.所有这些类型都没有.close方法(例如ServiceWorkers没有),因此该方法已移至每个特定类型的工作人员全局范围的定义中。

For instance you can see that for the DedicatedWorkerGlobalScope , it's still here, and isn't scheduled to be deprecated any time soon.例如,您可以看到DedicatedWorkerGlobalScope仍然存在,并且不会很快被弃用。

And it's actually also there in MDN .它实际上也存在于 MDN 中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM