欢迎来到山村网

HTML 5 Web开发:防止浏览器假死的方法

2019-03-30 09:25:04浏览:1011 来源:山村网   
核心摘要:一个浏览器至少存在三个线程:js引擎线程(处理js)、GUI渲染线程(渲染页面)、浏览器事件触发线程(控制交互)。JavaScript引擎是基

一个浏览器至少存在三个线程:js引擎线程(处理js)、GUI渲染线程(渲染页面)、浏览器事件触发线程(控制交互)。

Javascript引擎是基于事件驱动单线程执行的,JS引擎一直等待着任务队列中任务的到来然后加以处理,浏览器无论再什么时候都只有一个JS线程在运行JS程序。

GUI 渲染线程负责渲染浏览器界面,当界面需要重绘(Repaint)或由于某种操作引发回流(reflow)时,该线程就会执行。但需要注意 GUI渲染线程与JS引擎是互斥的,当JS引擎执行时GUI线程会被挂起,GUI更新会被保存在一个队列中等到JS引擎空闲时立即被执行。

事件触发线程,当一个事件被触发时该线程会把事件添加到待处理队列的队尾,等待JS引擎的处理。这些事件可来自Javascript引擎当前执行的代码块如setTimeOut、也可来自浏览器内核的其他线程如鼠标点击、AJAX异步请求等,但由于JS的单线程关系所有这些事件都得排队等待JS引擎处理。

了解了浏览器的内核处理方式就不难理解浏览器为什么会进入假死状态了,当一段JS脚本长时间占用着处理机就会挂起浏览器的GUI更新,而后面的事件响应 也被排在队列中得不到处理,从而造成了浏览器被锁定进入假死状态。另外JS脚本中进行了DOM操作,一旦JS调用结束就会马上进行一次GUI渲染,然后才 开始执行下一个任务,所以JS中大量的DOM操作也会导致事件响应缓慢甚至真正卡死浏览器,如在IE6下一次插入大量的HTML。而如果真的弹出了“脚本 运行时间过长“的提示框则说明你的JS脚本肯定有死循环或者进行过深的递归操作了。

现在如果遇到了这种情况,我们可以做的不仅仅是优化 代码,html5的webWorkers提供了js的后台处理线程的API,它允许将复杂耗时的单纯js逻辑处理放在浏览器后台线程中进行处理,让js线 程不阻塞UI线程的渲染。这个线程不能和页面进行交互,如获取元素、alert等。多个线程间也是可以通过相同的方法进行数据传递。

直接看代码:

例子:用户输入一个数字,进行加法运算(+=)

以前的做法:

  1. <!DOCTYPEHTML>
  2. <htmllang="en">
  3. <head>
  4. <metacharset="UTF-8">
  5. <title>webworkers--calculate</title></head>
  6. <body>
  7. <inputid="num"name="num"type="text"/>
  8. <buttononclick="calculate()">计算</button><br/>
  9. <divid="result"style="color:red;"></div>
  10. <divid="time"style="color:red;"></div>
  11. <scripttype="text/javascript"src=http://www.shancun.net/skin/default/image/nopic.gif style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; color: rgb(92, 92, 92); list-style-type: decimal; list-style-position: outside !important; list-style-image: none; word-wrap: break-word; word-break: normal; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat; "><scripttype="text/javascript">functioncalculate(){
  12. data1=newDate().getTime();
  13. varnum=document.getElementById("num").value;
  14. varval=parseInt(num,10);
  15. varresult=0;
  16. for(vari=0;i<num;i++){
  17. result+=i;
  18. }
  19. data2=newDate().getTime();
  20. document.getElementById("result").innerHTML="计算结果:"+result;
  21. document.getElementById("time").innerHTML="普通耗时:"+(data2-data1)+"ms";
  22. }
  23. </script>
  24. </body>
  25. </html>

使用webWorkers以后:

calculate.html

  1. <!DOCTYPEHTML>
  2. <htmllang="en"><head>
  3. <metacharset="UTF-8">
  4. <title>webworkers--calculate</title>
  5. </head>
  6. <body>
  7. <inputid="num"name="num"type="text"/>
  8. <buttononclick="calculate()">计算</button><br/>
  9. <divid="result"style="color:red;"></div>
  10. <divid="time"style="color:red;"></div>
  11. <scripttype="text/javascript"src=http://www.shancun.net/skin/default/image/nopic.gif style="margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; color: rgb(92, 92, 92); list-style-type: decimal; list-style-position: outside !important; list-style-image: none; word-wrap: break-word; word-break: normal; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; background-image: url(http://images.51cto.com/images/art1105/images/0.gif); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: transparent; line-height: 18px; background-position: -498px -70px; background-repeat: no-repeat repeat; "><scripttype="text/javascript">
  12. varworker=newWorker("calculate.js");
  13. vardata1=0;
  14. vardata2=0;
  15. worker.onmessage=function(event){
  16. vardata=event.data;
  17. data2=newDate().getTime();
  18. document.getElementById("result").innerHTML="计算结果:"+data;
  19. document.getElementById("time").innerHTML="workers耗时:"+(data2-data1)+"ms";
  20. };
  21. functioncalculate(){
  22. data1=newDate().getTime();
  23. varnum=document.getElementById("num").value;
  24. varval=parseInt(num,10);
  25. worker.postMessage(val);
  26. }
  27. </script>
  28. </body>
  29. </html>

calculate.js

  1. onmessage=function(event){
  2. varnum=event.data;
  3. varresult=0;
  4. for(vari=0;i<num;i++){
  5. result+=i;
  6. }
  7. postMessage(result);
  8. };

webWorker需要将代码放入web服务器中, 如果使用的是localhost请用高版本的chrome浏览器打开,firefox浏览器在处理localhost的时候会出现“Could not get domain!”的错误,关于这个可以参考:https://bugzilla.mozilla.org/show_bug.cgi?id=682450 对比上面的两种实现方式,当计算值达到100亿的时候,普通做法耗时已经很长,且一般会卡死了。

HTML 5 Web开发:防止浏览器假死的方法 山村

webWorkers在Chrome15下的效果

更正:getTime()返回的应该是毫秒(ms),而不是秒(s)。

如下图所示:

普通方法在Chrome15下的效果

可见webWorkers在未来的web应用中还是非常有价值的。

(责任编辑:豆豆)
下一篇:

国外开发的HTML 5精彩应用

上一篇:

HTML5游戏开发的5条实用建议

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com