網頁不在瀏覽器當前窗口,setInterval()運行有問題


  • 當網頁不在瀏覽器的當前窗口(或tab)許多瀏覽器限制setInteral()指定的反復運行的任務最多每秒執行一次。
  • 使用setInterval()的定時器會把事件運行的時間也包含在內,如果要精確算定時兩個任務之間的時間,可以使用setTimeout()替換。
  • // 用循環調用setTimeout模擬了setInterval
    function interval(func, wait){  
        var interv = function(){
            func.call(null);
            setTimeout(interv, wait);
        };
    
        setTimeout(interv, wait);
    }
    
    interval(function(){
        console.log(22222222);
    },1000);

     

 

參考鏈接:https://www.jeffjade.com/2016/01/10/2016-01-10-javaScript-setInterval/

參考鏈接:http://caibaojian.com/setinterval.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM