如何修复跳过每个点击按钮到倒计时的时间? (或每次点击重置倒计时)

How to fix time skipping every click button to countdown? (Or reset countdown every click)

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

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

如何让我每次点击倒计时按钮时重置倒计时计时器?

只需在您的初始化函数中清除它:

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