简体   繁体   English

计时器完成后,警报窗口会多次弹出

[英]Alert window popping up multiple times when timer finishes

I'm working on a simple timer using JS. 我正在使用JS工作一个简单的计时器。 When the timer hits 0, I want "time up!" 当计时器达到0时,我想要“时间到了!” to pop up. 弹出。

$(document).ready(function() {
    updateClock();
    var timeInterval = setInterval(updateClock(), 1000);
});

var ms = 5000;

function updateClock() {
    ms -= 1000;
    var minutes = Math.floor(ms / 1000 / 60);
    var seconds = Math.floor((ms / 1000) % 60);
    $('#minutes').html(minutes);
    $('#seconds').html(seconds);
    if (ms <= 0) {
        alert('Time is up!');
        clearInterval(timeInterval);
    };
}

Right now, the alert pops up multiple times. 现在,警报会多次弹出。 I suspect it's because I'm using clearInterval() incorrectly—when I open up the developer console, it says: 我怀疑是因为我正在使用clearInterval()错误 - 当我打开开发者控制台时,它说:

"Uncaught ReferenceError: timeInterval is not defined at updateClock." “未捕获的ReferenceError:updateClock中未定义timeInterval。”

However, I'm not sure what to change to make it function correctly. 但是,我不确定要改变什么才能使其正常运行。

Your variable timeInterval is inside $(document).ready(...) 你的变量timeInterval$(document).ready(...)

Try this: 尝试这个:

var timeInterval;

$(document).ready(function() {
    updateClock();
    timeInterval = setInterval(updateClock(), 1000);
});

var ms = 5000;

function updateClock() {
    ms -= 1000;
    var minutes = Math.floor(ms / 1000 / 60);
    var seconds = Math.floor((ms / 1000) % 60);
    $('#minutes').html(minutes);
    $('#seconds').html(seconds);
    if (ms <= 0) {
        alert('Time is up!');
        clearInterval(timeInterval);
    };
}

According to MDN doc : 根据MDN doc

var intervalID = window.setInterval(func, delay[, param1, param2, ...]); var intervalID = window.setInterval(func,delay [,param1,param2,...]);

var intervalID = window.setInterval(code, delay); var intervalID = window.setInterval(code,delay);

where: 哪里:

func FUNC

  A function to be executed every delay milliseconds. 

This means that if you call this function with first parameter like: 这意味着如果使用第一个参数调用此函数,例如:

updateClock()

the updateClock function is executed and the result code is returned to the setInterval function. 执行updateClock函数,并将结果代码返回给setInterval函数。 But, because such a function (ie: updateClock) returns undefined, the setInterval will have nothing to execute the next times. 但是,因为这样的函数(即:updateClock)返回undefined,所以setInterval下次没有任何内容可以执行。

For the other problem (ie: local variable) you may declare the variable "timeInterval" as a global function in this way: 对于另一个问题(即:局部变量),您可以通过以下方式将变量“timeInterval”声明为全局函数:

window.timeInterval = setInterval(updateClock, 1000);

This will assure the value of the global variaable (ie: timeInterval) is available to the timer handler immediately. 这将确保全局可变(即:timeInterval)的值立即可用于计时器处理程序。

The example: 这个例子:

 $(document).ready(function() { window.timeInterval = setInterval(updateClock, 1000); }); var ms = 10000; function updateClock() { ms -= 1000; var minutes = Math.floor(ms / 1000 / 60); var seconds = Math.floor((ms / 1000) % 60); $('#minutes').text(minutes); $('#seconds').text(seconds); if (ms <= 0) { setTimeout(function() { // this to refresh before alert alert('Time is up!'); }, 10); clearInterval(timeInterval); }; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span id="minutes">0</span>:<span id="seconds">10</span> 

You might also cache your min and sec dom elements instead to searches for them every time: 您也可以缓存min和sec dom元素,而不是每次都搜索它们:

 $(document).ready(function() { var minObj = $('#minutes'); var secObj = $('#seconds') window.timeInterval = setInterval(function () { updateClock(minObj, secObj); }, 1000); }); var ms = 10000; function updateClock(minObj, secObj) { ms -= 1000; var minutes = Math.floor(ms / 1000 / 60); var seconds = Math.floor((ms / 1000) % 60); minObj.text(minutes); secObj.text(seconds); if (ms <= 0) { setTimeout(function() { // this to refresh before alert alert('Time is up!'); }, 10); clearInterval(timeInterval); }; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span id="minutes">0</span>:<span id="seconds">10</span> 

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

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