Web Workers有哪些用例? [英] What are the use-cases for Web Workers?

查看:146
本文介绍了Web Workers有哪些用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用 Web Workers API 的真实世界。

解决方案


  • John Resig (jQuery成名)有一些有趣的使用网络的例子这里的工作人员 - 游戏,图形,加密。

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

  • 另一个实际用途:在Bespin中,他们使用Web Workers来执行语法突出显示,您在使用应用程序时不希望阻止您的代码编辑。 Mozilla :工作人员有用的一种方法是允许代码执行处理器密集型计算而不会阻塞用户界面线程。作为一个实际的例子,想象一个具有#s大表的应用程序(这是真实世界,顺便说一句 - 取自我在2年前编程的应用程序)。您可以通过输入字段更改表格中的一个#,并在相当密集的过程中重新计算不同列中的一堆其他数字。

    旧的工作流程是:更改#。当JavaScript通过对其他数字的更改进行处理时,获取咖啡并且网页在3分钟内没有响应 - 在我将其优化为地狱之后。回来喝咖啡。更换第二个#。重复很多次。点击保存按钮。



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

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

    解决方案

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

    这篇关于Web Workers有哪些用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆