我浏览了几篇关于 HTML5 中的 Web Workers 的文章,但我无法清楚地理解以下内容:
- Web Worker 的生命周期是否仅限于单个页面或域的跨页面?
- Web Worker 的生命周期是否在页面加载后保持不变?
- 与 setInterval 中的 ajax 调用相比,网络 worker 是否是更好的选择?
如果有人可以帮助我阐明上述问题,我将不胜感激。
最佳答案
- Web Worker 的生命周期是否仅限于单个页面或域的跨页面?
Dedicated worker is limited to a single page, while shared worker can be related to many webpages
- Web Worker 的生命周期是否在页面加载后保持不变?
It could be persistent if it has onmessage() defined, and it could also returns after execution, if I understand correctly. In spec it says: let that run until it either returns, fails to catch an exception, or gets prematurely aborted by the "kill a worker" or "terminate a worker"...
- 与 setInterval 中的 ajax 调用相比,网络 worker 是否是更好的选择?
It depends. You can't simply say it is better or not. At the moment Web worker is mainly used for offloading complex JavaScript algorithms to run in the background.
关于multithreading - HTML5 Web Worker 是针对一个页面还是跨页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9829620/