我正在学习,所以请客气:-)
我找到了完美的倒计时时钟。
我要实现的是在1
字之后出现Lift Off!
,但是由于某种原因,倒数时钟一直显示1
var startNum;
var currentNum;
function addClassDelayed(jqObj, c, to) {
setTimeout(function() { jqObj.addClass(c); }, to);
}
function anim() {
addClassDelayed($("#countdown"), "puffer", 600);
if (currentNum == 0) $('#countdown').html("Lift Off!"); else currentNum--;
$('#countdown').html(currentNum+1);
$('#countdown').removeClass("puffer");
}
$(function() {
startNum = 5;
currentNum = startNum;
$("#countdown").html(currentNum); // init first time based on n
self.setInterval(function(){anim()},1325);
});
最佳答案
$('#countdown').html(currentNum+1);
覆盖您之前设置的任何内容。
function anim() {
addClassDelayed($("#countdown"), "puffer", 600);
if (currentNum == 0) {
$('#countdown').html("Lift Off!");
clearInterval(token)
} else {
currentNum--;
$('#countdown').html(currentNum+1); // Only show if not "Lift Off!"
}
$('#countdown').removeClass("puffer");
}
var token;
$(function() {
startNum = 5;
currentNum = startNum;
$("#countdown").html(currentNum); // init first time based on n
token = self.setInterval(function(){anim()},1325);
});
根据Tyler的建议添加了
clearInverval