Web worker的用例是什么?

时间:2021-02-25 19:34:59

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

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

3 个解决方案

#1


119  

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

    John Resig(以jQuery闻名)在这里有很多有趣的使用web worker的例子——游戏、图形、密码。

  • 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.

    另一个用途是Web I/O——换句话说,在后台轮询url。这样就不会阻塞等待轮询结果的UI。

  • 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.

    另一个实际用途是:在Bespin中,他们使用Web Workers来做语法高亮显示,在使用应用程序时,您不会想要阻止代码编辑。

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

    Mozilla:一个有用的方法是允许代码执行处理器密集型的计算,而不阻塞用户界面线程。

    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.

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

    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.

    旧的工作流程是:更改#。当JavaScript通过对其他数字的改变而进行处理时,你可以去喝咖啡,而web页面的响应时间为3分钟——在我将它优化为hell和back之后。回到与咖啡。改变第二个#。重复很多次。单击SAVE按钮。

    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".

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

#2


31  

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调用来实现这一点,但是如果这占用了每个主机名的一个宝贵的连接的话。此外,如果用户在此过程中进行页面转换(e)。g点击一个链接),你的JavaScript对象从上一页消失,你不能处理回调。当使用web worker时,该活动会发生在带外,因此您可以更好地保证它将完成。

#3


6  

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.

如果您有很多图片和其他媒体文件,可以从服务器以压缩格式交换,则在后台压缩/解压文件。

#1


119  

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

    John Resig(以jQuery闻名)在这里有很多有趣的使用web worker的例子——游戏、图形、密码。

  • 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.

    另一个用途是Web I/O——换句话说,在后台轮询url。这样就不会阻塞等待轮询结果的UI。

  • 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.

    另一个实际用途是:在Bespin中,他们使用Web Workers来做语法高亮显示,在使用应用程序时,您不会想要阻止代码编辑。

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

    Mozilla:一个有用的方法是允许代码执行处理器密集型的计算,而不阻塞用户界面线程。

    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.

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

    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.

    旧的工作流程是:更改#。当JavaScript通过对其他数字的改变而进行处理时,你可以去喝咖啡,而web页面的响应时间为3分钟——在我将它优化为hell和back之后。回到与咖啡。改变第二个#。重复很多次。单击SAVE按钮。

    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".

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

#2


31  

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调用来实现这一点,但是如果这占用了每个主机名的一个宝贵的连接的话。此外,如果用户在此过程中进行页面转换(e)。g点击一个链接),你的JavaScript对象从上一页消失,你不能处理回调。当使用web worker时,该活动会发生在带外,因此您可以更好地保证它将完成。

#3


6  

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.

如果您有很多图片和其他媒体文件,可以从服务器以压缩格式交换,则在后台压缩/解压文件。