[英]How to stop JavaScript in Web Worker from the outside?
I am calling a JavaScript function through a onclick
event from my webpage, which on event will start running synchronously in my Worker
.我正在通过网页上的
onclick
事件调用 JavaScript 函数,该事件将开始在我的Worker
同步运行。 I want to know if there is any way to stop that JS function after getting the desired result through onclick
event ie manually from my webpage.我想知道在通过
onclick
事件(即从我的网页手动获得所需结果)后,是否有任何方法可以停止该 JS 函数。
So I wanted to know if there is any way to stop that js function after getting the desired result...
所以我想知道在获得所需的结果后是否有任何方法可以停止该js功能......
If you have the desired result, the web worker shouldn't still be running.如果您获得了所需的结果,则 Web Worker 不应仍在运行。 It should terminate (or go dormant) after sending the result to the main page.
在将结果发送到主页后,它应该终止(或进入休眠状态)。 It might still have a
message
handler registered (so you can tell it to do something again), but it shouldn't still be running .它可能仍然注册了一个
message
处理程序(因此您可以告诉它再次执行某些操作),但它不应仍在运行。
How to stop Javascript in webworker from outside?
如何从外部停止 webworker 中的 Javascript?
If the web worker is continuously running code such as a tight loop that never yields back to the browser, you can terminate it via the terminate
method on the Worker
instance:如果 Web Worker 持续运行代码,例如永远不会返回给浏览器的紧密循环,您可以通过
Worker
实例上的terminate
方法终止它:
Where you start the worker:从哪里开始工作:
var worker = new Worker("yourscript.js");
Later, when you want to kill it:后来,当你想杀死它时:
worker.terminate();
If the worker yields back to the browser periodically (via setTimeout
, or just because it's waiting for a message
with the next thing to work on), you can post it an application-specific message telling it to clean up and terminate.如果 worker 定期让步给浏览器(通过
setTimeout
,或者只是因为它正在等待下一个要处理的message
),您可以向它发布一条特定于应用程序的消息,告诉它清理和终止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.