[英]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.