如何在时间结束时显示警报并且计时器应该停止运行我已经尝试了以下代码但是计时器继续运行并且它没有显示任何警告消息帮助我
<script>
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.text(minutes + ":" + seconds);
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
jQuery(function ($) {
var fiveMinutes = 60 *0.10,
display = $('#time');
startTimer(fiveMinutes, display);
if(startTimer<0)
{
alert("time has ended");
}
});
</script>
<div style="float-left">Test Will Ends in <span id="time">10:00</span>
答案 0 :(得分:1)
首先,您必须将间隔附加到变量,例如:
interval = setInterval(function() { ... }, 1000);
然后检查结束时间并使用clearInterval()
:
if (--timer < 0) {
timer = duration;
window.alert("Time's up!");
clearInterval(interval);
}
答案 1 :(得分:1)
试试这个:
jQuery(function ($) {
var fiveMinutes = 60 *10,
display = $('#time');
startTimer(fiveMinutes, display);
});
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
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.text(minutes + ":" + seconds);
if (--timer < 0) {
alert("text!!!!"); // this will show the alert popup
clearInterval(interval); // this will stop the timer
}
}, 1000);
}