简体   繁体   English

Web 页面卡住,而 web 工作人员面临无限循环

[英]Web page stuck while the web worker faced an endless loop

I am working on a web project that needs quite a few calculations, and I use a web worker to do so.我正在处理一个需要大量计算的 web 项目,我使用 web 工作人员来执行此操作。 There is also a button on the web page that should terminate the worker's calculation whenever the user clicked it. web 页面上还有一个按钮,只要用户单击该按钮,该按钮就会终止工作人员的计算。 However, when I wrote an endless loop inside the web worker to test, the webpage stuck and I cannot interact with it.但是,当我在 web 工作程序中编写一个无限循环进行测试时,网页卡住了,我无法与之交互。

 //When the worker is created worker = new Worker("./scripts/testWorker.js"); //When the button is clicked worker.terminate(); worker = undefined;

How to solve this problem?如何解决这个问题呢? Or are there any other ways to reach the goal of performing breakable calculations?或者还有其他方法可以达到执行易碎计算的目标吗?

In the endless loop of the worker thread, there is a postMessage function.在工作线程的死循环中,有一个postMessage function。 Hence, the onmessage function in the main thread is called rapidly.因此,主线程中的onmessage function 被快速调用。 Therefore, the main thread stuck when the worker starts.因此,当worker启动时,主线程卡住了。 Deleting this postMessage inside the endless loop of the worker thread solved this problem.在工作线程的无限循环中删除这个postMessage解决了这个问题。

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

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