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’t want 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".
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.
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.
发布评论
评论(3)
John Resig</a>(jQuery 名人)拥有 这里有一堆使用 Web Worker 的有趣示例 (镜像) - 游戏、图形、加密货币。
另一个用途是 Web I/O - 换句话说,在后台轮询 URL。这样您就不会阻止 UI 等待轮询结果。
另一个实际用途:在 Bespin 中,他们使用 Web Workers 来进行语法突出显示,您不会希望在使用应用程序时阻止代码编辑。
来自Mozilla:工作人员有用的一种方式是允许您的代码执行处理器 -密集计算而不阻塞用户界面线程。
作为一个实际的例子,想象一个有一个很大的 # 表的应用程序(这是现实世界,顺便说一句 - 取自我大约 2 年前编写的一个应用程序)。您可以通过输入字段更改表中的一个#,并且不同列中的一堆其他数字会在相当密集的过程中重新计算。
旧的工作流程是:更改#。去喝杯咖啡,而 JavaScript 正在处理对其他数字的更改,并且网页在 3 分钟内没有响应 - 在我对它进行了地狱般的优化之后。喝着咖啡回来。更改第二个#。重复多次。单击“保存”按钮。
工作人员的新工作流程可能是:更改#。获取一条状态消息,表明某些内容正在重新计算,但您可以更改其他#。更改更多#。完成更改后,等待状态更改为“所有计算完成,您现在可以查看最终的#并保存”。
John Resig (of jQuery fame) has a bunch of interesting examples of using web workers here (mirror) - 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’t want 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".
我使用它们从浏览器向服务器发送大量数据。显然,您可以通过常规 AJAX 调用来完成此操作,但如果这占用了每个主机名的宝贵连接之一。此外,如果用户在此过程中进行页面转换(例如单击链接),则前一页中的 JavaScript 对象将消失,并且您无法处理回调。当使用网络工作者时,此活动发生在带外,因此您可以更好地保证它会完成。
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.
另一个用例:
如果您有大量以压缩格式从服务器交换的图像和其他媒体文件,则在后台压缩/解压缩文件。
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.