0
我在我的网站上有一个测验,如果时间到了,它会提交。表单提交后(通过jQuery),我想定时器停止工作。测试提交后,我尝试了return: false
,但这并不奏效。从2个Javascript循环中退出(或停止脚本)
HTML:
<span id="time">30:00</span>
的javascript:
var thirtyMinutes = 60 * 30,
display = document.querySelector('#time');
startTimer(thirtyMinutes, display);
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
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 ($('#time').html() == '00:00'){
alert("There is no time left remaining. The test will now submit.");
$('#submitTest').trigger("click"); //submit the test
//this is where I would like to exit the script and stop the timer from working
}
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
'setTimeout'而不是'setInterval'? – PSkocik