Javascript在某些条件为真时停止时间?

时间:2018-04-12 12:08:29

标签: javascript

以下是测验的JavaScript倒计时代码:

var isClicked;
    isClicked = false;
    document.getElementById('quiz_button').onclick = function() {
    isClicked = true;
};

function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    const interval = setInterval(function() {
        minutes = parseInt(timer / 60, 10)
        seconds = parseInt(timer % 60, 10);
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;
        display.textContent = minutes + ":" + seconds;
        if (timer !== 0) {
            --timer;
            <?php
            // start the time session...
            Session::set('startTime', time());
            ?>
        } else if (timer == 0) {
            if( isClicked == false ) {
                $( "#quiz_button" ).trigger( "click", function () {
                    clearInterval(interval);
                });
            } else {
                clearInterval(timer);
            }
        }
    }, 1000);
}

window.onload = function () {
    var seconds = 10,
        display = document.querySelector('#time');
        startTimer(seconds, display);
};

页面加载后开始倒计时。

有一个quiz_button来完成测验。单击它时,我使用JS设置它true。否则,它会持续触发按钮。

但是当按下按钮完成测验时,倒计时仍在计算中。我需要阻止它。我怎么能阻止它?

我做了:

} else {
    clearInterval(timer);
}

0 个答案:

没有答案