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

后端 未结 2 1182
鱼传尺愫
鱼传尺愫 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();
    }
    

提交回复
热议问题