亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

setTimeout的機制引起的bug

setTimeout的機制引起的bug

慕田峪7331174 2018-09-05 14:15:16
瀏覽器打開了2個標簽頁,其中一個標簽頁A中有如下代碼:`    var time = 0;    var i = 0;    var t = 0;    function test(){         i++;         setTimeout(function(){             $('body').append('-'+i);             test();         },100);     }     test();    `如果我一直停留在這個頁面上,程序沒有任何問題,會不斷的在頁面上打印遞增的數字 ,但是當我點擊另一個標簽頁B時,標簽頁A中的代碼會停止執行,直到我重新切換到標簽頁A時,代碼又會繼續執行。這個問題不知道要怎么解決,求大神幫忙感激不盡!PS.經過測試,發現這個問題只有在chrome和firefox才會出現,IE正常。再PS.上面說的停止執行是我的視覺誤判,真實原因如下:On the Chromium blog, Google said: In the forthcoming Chrome 11 release, we plan to reduce CPU consumption even for pages that are using setTimeout and setInterval. For background tabs, we intend to run each independent timer no more than once per second. This change has already been implemented in the Chrome dev channel and canary builds.解決辦法:使用webworker(兼容性不夠好)或者使用這個補丁 https://github.com/turuslan/H...
查看完整描述

1 回答

?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

參考這里:傳送門

原文:

Convert background page to event page

Follow this checklist to convert your extension's (persistent) background page to an event page.

Add "persistent": false to your manifest as shown above.
If your extension uses window.setTimeout() or window.setInterval(), switch to using the alarms API instead. DOM-based timers won't be honored if the event page shuts down.
Similarly, other asynchronous HTML5 APIs like notifications and geolocation will not complete if the event page shuts down. Instead, use equivalent extension APIs, like notifications.
If your extension uses, extension.getBackgroundPage, switch to runtime.getBackgroundPage instead. The newer method is asynchronous so that it can start the event page if necessary before returning it.

英語不是特別好,就不做中間翻譯了。


查看完整回答
反對 回復 2018-10-29
  • 1 回答
  • 0 關注
  • 756 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號