jQuery/[removed] My recursive setTimeout function speeds up when tab becomes inactive

后端 未结 2 1172
鱼传尺愫
鱼传尺愫 2020-12-04 02:22

I\'ve got an odd little dilemma in this jQuery slideshow plugin that I am building.

It\'s nothing fancy and the code I have written to date is working great however

相关标签:
2条回答
  • 2020-12-04 02:40

    Instead of timeouts have you tried intervals? Also for it to be recursive, just call the nextSlide() function as its own callback:

    var counter = 1;
    
    // animate to the next slide
    function nextSlide() {
    
        // increase counter
        counter++;
    
        // if counter is greater than the amount of slides, back to the start.
        counter =  ( counter > slides.length-1 ) ? 0 : counter;
    
        // inner = container to be animated
        // in the complete callback restart the timer.
        inner.animate(
        {
            'left': '-' + slides.eq( counter ).position().left
        }, 
        {
            duration : settings.animationSpeed,
            easing  : 'easeInOutExpo',
            complete : nextSlide()
        });
    
    }
    

    Then it's just a matter of starting and stopping an interval:

    var slideshow;
    function startSlideshow()
    {
        slideshow = setInterval(nextSlide(),3000);
    }
    
    function stopSlideshow()
    {
        clearInterval(slideshow);
        inner.stop();
    }
    
    0 讨论(0)
  • 2020-12-04 02:47

    Some browsers (like Chrome) drastically slow down recurring timers when the tab goes inactive and then, when the tab comes active again, they try to "catch up" so that the same number of actual timer events has occurred. All I can think of as a work-around is for you to stop the slideshow entirely when the tab goes inactive and start it again when it comes active.

    0 讨论(0)
提交回复
热议问题