Javascript Web Workers 的用例是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2773682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:52:36  来源:igfitidea点击:

What are the use-cases for Web Workers?

javascripthtmlweb-worker

提问by Sergey Ilinsky

I am looking for real-world scenarious for using Web Workers API.

我正在寻找使用Web Workers API 的真实场景。

采纳答案by DVK

  • John Resig(of jQuery fame) has a bunch of interesting examples of using web workers here- games, graphics, crypto.

  • Another use is Web I/O - in other words, polling URLs in background. That way you don't block the UI waiting for polling results.

  • Another practical use: in Bespin, they're using Web Workers to do the syntax highlighting, which you wouldn'twant to block your code editing whilst you're using the app.

  • From Mozilla: One way workers are useful is to allow your code to perform processor-intensive calculations without blocking the user interface thread.

    As a practical example, think of an app which has a large table of #s (this is real world, BTW - taken from an app I programmed ~2 years ago). You can change one # in a table via input field and a bunch of other numbers in different columns get re-computed in a fairly intensive process.

    The old workflow was: Change the #. Go get coffee while JavaScript crunches through changes to other numbers and the web page is unresponsive for 3 minutes - after I optimized it to hell and back. Get Back with coffee. Change a second #. Repeat many times. Click SAVE button.

    The new workflow with the workers could be: Change the #. Get a status message that something is being recomputed but you can change other #s. Change more #s. When done changing, wait till status changes to "all calculations complete, you can now review the final #s and save".

  • John Resig(因 jQuery 出名)有很多在这里使用 Web Worker 的有趣示例- 游戏、图形、加密。

  • 另一个用途是 Web I/O - 换句话说,在后台轮询 URL。这样您就不会阻止 UI 等待轮询结果。

  • 另一个实际用途:在 Bespin 中,他们使用 Web Workers 进行语法高亮显示,您希望在使用应用程序时阻止您的代码编辑。

  • 来自Mozilla:worker 有用的一种方式是允许您的代码执行处理器密集型计算,而不会阻塞用户界面线程。

    作为一个实际的例子,想想一个应用程序,它有一个很大的#s 表(这是现实世界,顺便说一句 - 取自我大约 2 年前编写的应用程序)。您可以通过输入字段更改表中的一个#,并且在相当密集的过程中重新计算不同列中的一堆其他数字。

    旧的工作流程是:更改#。去喝咖啡,而 JavaScript 正在处理其他数字的更改,并且网页在 3 分钟内没有响应 - 在我对其进行了优化并返回之后。回来喝咖啡。更改第二个#。重复多次。单击“保存”按钮。

    工作人员的新工作流程可能是:更改#。获取正在重新计算某些内容的状态消息,但您可以更改其他 #s。更改更多#s。完成更改后,等待状态更改为“所有计算完成,您现在可以查看最终的#s 并保存”。

回答by Kevin Hakanson

I have used them for sending larger amounts of data from the browser to server. Obviously, you can do this with regular AJAX calls, but if this takes up one of the precious connections per hostname. Also, if the user does a page transition during this process (e.g clicks a link), your JavaScript objects from the previous page go away and you can't process callbacks. When a web worker is used, this activity happens out of band, so you have a better guarantee that it will complete.

我已经使用它们将大量数据从浏览器发送到服务器。显然,您可以使用常规的 AJAX 调用来做到这一点,但如果这占用了每个主机名的宝贵连接之一。此外,如果用户在此过程中进行了页面转换(例如单击链接),则上一页中的 JavaScript 对象将消失,并且您无法处理回调。使用 Web Worker 时,此活动在带外发生,因此您可以更好地保证它会完成。

回答by sbr

Another Use case:

另一个用例:

Compressing/De-compressing files in the background, if you have a lot of images and other media files that are exchanged from the server in compressed format.

在后台压缩/解压缩文件,如果您有大量从服务器以压缩格式交换的图像和其他媒体文件。