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

原文地址:https://www.cnblogs.com/laq627/p/13808929.html