我试图在动画完成后启动一个功能,但无法使其工作。
这是我的代码:
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) {});
我尝试过:
$('.begin-mrsuper').delay(500).animate({"left":"4px"}, "slow").promise().done(function(){ handleTimer(); });
但是 3,2,1,GO 计时器在动画结束之前启动,有什么想法吗?
最佳答案
将setInterval
部分包装到一个函数中,否则一旦调用它就会执行。
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);
关于jquery .animate() 和 .done(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22547888/