jquery .animate() and .done()

走远了吗. 提交于 2019-12-11 02:48:28

问题


I'm trying to fire up a function after the animation has finished but i can't make it work.

Here is my code:

var count = 3;
var timer = setInterval(function () {
    handleTimer(count);
}, 1000);

function endCountdown() {
    $('.begin-btn').html("GO!");
}

function handleTimer() {
    if (count === 0) {
        clearInterval(timer);
        endCountdown();
    } else {
        $('.begin-btn').html(count);
        count--;
    }
}


$('.begin-mrsuper').delay(500).animate({
    "left": "4px"
}, "slow").promise().done(function (handleTimer) {});

I tryed and:

    $('.begin-mrsuper').delay(500).animate({"left":"4px"}, "slow").promise().done(function(){ handleTimer(); });

But the 3,2,1, GO timer starts before the animation finishes, any ideas?


回答1:


Wrap the setInterval part into a function, or it will be executed once you called it.

var count = 3, timer = null;
var foo = function() {
    timer = setInterval(function () {
        handleTimer(count);
    }, 1000);
};

function endCountdown() {
    $('.begin-btn').html("GO!");
}

function handleTimer() {
    if (count === 0) {
        clearInterval(timer);
        endCountdown();
    } else {
        $('.begin-btn').html(count);
        count--;
    }
}


$('.begin-mrsuper').delay(500).animate({
    "left": "4px"
}, "slow").promise().done(foo);



回答2:


Something like this might just work:

$('.begin-mrsuper').delay(500).animate({
    "left": "4px"
}, "slow", function(){ handleTimer(); }).promise();


来源:https://stackoverflow.com/questions/22547888/jquery-animate-and-done

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!