我将倒数计时器设置为3秒(当它为零时显示警报),在将其设置为0之前再次单击它,计时器的运行速度非常快。
$(".change-word").click(function(e) {
$(".countdown").html("0");
$(".countdown").html("3");
var doUpdate = function() {
$('.countdown').each(function() {
var count = parseInt($(this).html());
if (count !== 0) {
$(this).html(count - 1);
} else {
alert("zero");
}
});
};
setInterval(doUpdate, 1000);
});
这是HTML
<div class="countdown">3</div>
当计时器达到0时,您不会:
这样,即使您没有在警报上单击“确定”,它也总是每1000毫秒循环一次,这就是为什么它看起来“更快”的原因。
If you replace the alert with a
console.log(count)
you'll see it will write 0 every second.