如果我点击一次,我的倒计时会正常运行。但如果我多次点击它,它会跳过时间。这是我的代码:
<input type="submit" value="Countdown" id="countdown" onclick="countdown_init()" />
<div id="countdown_text"></div>
<script type="application/javascript">
var countdown;
var countdown_number;
var number = document.getElementById('countdown_text');
function countdown_init() {
countdown_number = 6;
countdown_trigger();
}
function countdown_trigger() {
if(countdown_number > 0) {
countdown_number--;
number.innerHTML = countdown_number;
if(countdown_number > 0 ) {
countdown = setTimeout('countdown_trigger()', 1000);
}
}
if(number.innerHTML == "0"){
document.getElementById('countdown_text').innerHTML = "";
}
}
</script>
如果每次点击倒计时按钮,如何重置倒数计时器?
答案 0 :(得分:2)
在init函数中清除它:
function countdown_init() {
clearTimeout(countdown);
countdown_number = 6;
countdown_trigger();
}