When using setInterval, if I switch tabs in Chrome and go back, the slider goes crazy catching up

前端 未结 6 699
醉话见心
醉话见心 2020-11-28 05:05

I have a jQuery slider on my site and the code going to the next slide is in a function called nextImage. I used setInterval to run my function on a timer, and it does exact

6条回答
  •  佛祖请我去吃肉
    2020-11-28 05:22

    I post an answer here: How can I make setInterval also work when a tab is inactive in Chrome?

    Just do this:

    setInterval(function() {
        $("#your-image-container").stop(true,true);
        nextImage();
    
    }, 1000);
    

    inactive browser tabs buffer some of the setInterval or setTimeout functions. stop(true,true) - will stop all buffered events and execute immadietly only last animation.

    The window.setTimeout() method now clamps to send no more than one timeout per second in inactive tabs. In addition, it now clamps nested timeouts to the smallest value allowed by the HTML5 specification: 4 ms (instead of the 10 ms it used to clamp to).

提交回复
热议问题