Is setInterval() and setTimeout() bad things to do in modern jQuery animations?

后端 未结 1 787
北恋
北恋 2020-11-30 14:34

I had some problems with animations with jQuery 1.6. I solved it with jQuery 1.5.

In my project I used setInterval() to make custom logo slider. Animati

相关标签:
1条回答
  • 2020-11-30 14:56

    In general setInterval == BAD and setTimeout == GOOD for animations.

    setInterval will try play catchup, as nnnnnn stated:

    some browsers may queue everything and then try to catch up when your tab gets focus again

    You best method for looping animate() is by calling recursively, for example:

    var myTimeout;
    
    var myAnimation = function () {
    
        $('#myID').animate({
            [propertyKey]:[propertyValue]
        }, 5000, function() {
            myTimeout = setTimeOut(myAnimation, 1000);
        });
    }
    

    Notice how the myTimeout is held outside the scope of myAnnimation allowing the ability to stop the animation

    clearTimeout(myTimeout);
    

    Which you could hook up to the window.unload event.

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