网页不在浏览器当前窗口,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