以下是测验的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);
}