通过Web Workers优雅地降级 [英] Degrading gracefully with Web Workers

查看:111
本文介绍了通过Web Workers优雅地降级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我开始越来越多地听到有关网络工作者的信息.我认为这绝对很棒,但是到目前为止,我还没有看到任何人真正解决过的问题是如何支持尚未支持新技术的旧版浏览器.

So I'm starting to hear more and more about Web Workers. I think it's absolutely fantastic, but the question I haven't seen anyone really tackle so far is how to support older browsers that do not yet have support for the new technology.

到目前为止,我唯一能想到的解决方案是围绕Web Worker功能进行某种包装,这些包装可以归结为一些疯狂的基于计时器的解决方案,以模拟多线程执行.

The only solution I've been able to come up with so far is to make some sort of wrapper around the web worker functionality that would fall back to some crazy timer based solution that would simulate multi-threaded execution.

但是即使在这种情况下,如何检测Web Worker是否是当前正在执行javascript的浏览器的受支持功能?

But even in that case, how does one detect whether web workers is a supported feature of the browser currently executing the javascript?

谢谢!

推荐答案

这是一个古老的Web开发问题:如何处理不支持所需功能的浏览器.当前,我只主张将Web Workers用于复杂的,长期运行的任务,这些任务可以分解,并且由于某种原因不能在服务器端完成.这样,如果您没有Web Worker,则只需等待更长的时间.否则,您将不得不使用包装程序弄乱您的代码,而稍后您将尝试避免这种情况.页面加载后,我就会执行降级策略.

This is the age-old problem of web development: what to do about browsers that don't support what you need. Currently, I only advocate using Web Workers for complex, long-running tasks that can be broken up, and for some reason, can't be done server-side. This way, if you don't have Web Workers, you simply wait longer. Otherwise, you would have to make a mess of your code with wrappers and whatnot that you'll try to avoid later. My degradation strategy happens as soon as the page is loaded.

onload函数伪代码:

onload function pseudocode:

if( window.Worker /*check for support*/ )
    someObject.myFunction = function() { /*algorithm that uses Web Workers*/ }
else
    someObject.myFunction = function() { /* sad face */ }

您仍然必须两次编写算法,但是如果要支持不带Web Workers的浏览器,则无论如何都必须这样做.这就引出了一个有趣的问题:写两次东西值得花时间(和金钱),这样对某些人来说可以更快一点吗?

You still have to write the algorithm twice, but you would have to do that anyway if you want to support browsers without Web Workers. So that brings up an interesting question: is it worth the time (and money) to write something twice, just so it can go a little faster for some people?

这篇关于通过Web Workers优雅地降级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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