如何确定时间跳过每个点击按钮倒计时? (或每次点击重设倒计时)

时间:2015-06-23 21:03:08

标签: javascript html button countdown

如果我点击一次,我的倒计时会正常运行。但如果我多次点击它,它会跳过时间。这是我的代码:

<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>

如果每次点击倒计时按钮,如何重置倒数计时器?

1 个答案:

答案 0 :(得分:2)

在init函数中清除它:

function countdown_init() {
    clearTimeout(countdown);
    countdown_number = 6;
    countdown_trigger();
}