简体   繁体   English

setInterval计时器功能不起作用

[英]setInterval timer function not working

I'm making a countdown timer. 我在做倒数计时器。 When I take out the setInterval() function, and decrement the time without pausing, the program works correctly. 当我取出setInterval()函数并减少时间而不暂停时,该程序可以正常工作。

But when I use the setInterval() function to call the code that decrements my time only every second, the program hangs. 但是,当我使用setInterval()函数调用仅每秒减少我的时间的代码时,程序将挂起。

function timeControl() {
   var minutesLeft = $('#minutes').text();
   var secondsLeft = $('#seconds').text();

    while(Number(minutesLeft) > 0 || Number(secondsLeft) > 0) {
        window.setInterval(decreaseTime, 1000);

        minutesLeft = $('#minutes').text();
        secondsLeft = $('#seconds').text();
    }
 }


function decreaseTime() {
   var secondsLeft = $('#seconds').text();
   var minutesLeft = $('#minutes').text();
   if((Number(minutesLeft) > 0) && (Number(secondsLeft) == 0)) {
     $('#minutes').text(Number(minutesLeft) - 1);
     $('#seconds').text(4);
    } else {
       $('#seconds').text(Number(secondsLeft) - 1);
    }
 }

You add while and it looping very fast, so you setInterval() calling many times, so browser hanging. 您添加while并使其循环非常快,因此您多次调用setInterval() ,因此浏览器挂起。 You don't need to while, because setInterval run every time that defined in setInterval second parameter. 您不需要setInterval ,因为setInterval每次都会在setInterval第二个参数中定义的时间运行。 You can use following code: 您可以使用以下代码:

var decreaseTimeInterval;
function timeControl() {
    decreaseTimeInterval = window.setInterval(decreaseTime, 1000);
}

function decreaseTime() {
    var secondsLeft = $('#seconds').text();
    var minutesLeft = $('#minutes').text();
    if((Number(minutesLeft) > 0) && (Number(secondsLeft) == 0)) {
        $('#minutes').text(Number(minutesLeft) - 1);
        $('#seconds').text(4);
    } else if(Number(minutesLeft) == 0 && Number(secondsLeft) == 0) { //end of script
        clearInterval(decreaseTimeInterval); //clear interval to prevent do again
    } else {
        $('#seconds').text(Number(secondsLeft) - 1);
    }
}

You appear to be conflating the asynchronous setInterval function with a synchronous sleep kind of function. 您似乎正在将异步setInterval函数与同步sleep类函数混为一谈。 They are totally different. 他们是完全不同的。 The setInterval function sets a timer for you. setInterval函数为您设置一个计时器。 It says, "run function x every y milliseconds". 它说:“每y毫秒运行一次函数”。 But here, you are setting that function umpteen different times in that while loop. 但是在这里,您需要在while循环中将该功能设置为无数次。 Essentially, you start setting off more and more timers, hundreds or thousands of them, each one running the decreaseTime function every 1 second, until the program hangs. 本质上,您开始启动越来越多的计时器,其中数百或数千个计时器,每个计时器每1秒运行一次decreaseTime函数,直到程序挂起。

What you want to do is put the setInterval function at the top level of your program, and have it run the timeControl function every 1000 ms. 您要做的是将setInterval函数放在程序的顶层,并让它每1000毫秒运行一次timeControl函数。 Remove the while loop from the timeControl function -- you don't need it since the setInterval function itself is running a loop for you. timeControl函数中删除while循环-您不需要它,因为setInterval函数本身正在为您运行一个循环。 However, you will need to save the return value of setInterval in a variable so that you can clear it when your stop condition is reached. 但是,您需要将setInterval的返回值保存在变量中,以便在达到停止条件时可以将其清除。 In short, read more about setInterval : it's a totally different concept from sleep . 简而言之,请阅读有关setInterval更多信息:这是与sleep完全不同的概念。

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

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