繁体   English   中英

如何在jquery时间结束时发出警报?

[英]how to alert when the time has ended in jquery?

如何在时间结束且计时器应停止运行时显示警报,我尝试了以下代码,但是计时器保持运行状态,并且不显示任何警报消息

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

首先,您必须时间间隔附加到变量,例如:

interval = setInterval(function() { ... }, 1000);

然后检查结束时间并使用clearInterval()

if (--timer < 0) {
    timer = duration;
    window.alert("Time's up!");
    clearInterval(interval);
}

演示

尝试这个:

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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM