setTimeout speeds up with multiple tabs

后端 未结 7 1604
慢半拍i
慢半拍i 2020-12-10 07:47

I’m having a setTimeout problem similar to this one. But that solution doesn\'t help me since I can’t use php in my file.

My site has a slider with a list of images

7条回答
  •  独厮守ぢ
    2020-12-10 08:11

    I finally found my answer and it’s not at all what I was expecting. It seems the culprit is jQuery’s .animate(), which I use to move the images in the slider.

    I calculate and move my images positions with this:

    $('.spotlight-inner')
        .animate(
            { left: scrollToVal },
            {duration: 'slow'}
        )
     ;
    

    Now the problem seems to be that in some browsers, after you switch to a new tab and back, jQuery’s .animate() saves up the animations and fires them all at once. So I added a filter to prevent queueing. That solutions comes from CSS-Tricks.com :

    $('.spotlight-inner')
        .filter(':not(:animated)')
        .animate(
            { left: scrollToVal },
            {duration: 'slow'}
        )
    ;
    

    The first slide you see when you go back can act a little jumpy but it’s better than the superspeed carousel from before.

    Fiddle with the full code here

提交回复
热议问题