2015-09-08 34 views
2

我正在一个计时器,并在一些答案后,我得到了在这个论坛它一直工作顺利。这是什么样子,现在(只是让你的想法)无限循环在没有一个不透明的原因

enter image description here

我的代码(请注意,这是在这样一些东西没有完全结束正在进行的工作,基本上每仍然没有被使用,有一个alert("workin");功能

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 

 
<html> 
 

 
<head> 
 

 
<title>WIP</title> 
 
<meta charset="UFT-8"> 
 
<link rel="stylesheet" href="style.css"> 
 
<script src="script.js"></script> 
 

 

 
<script> 
 

 
$(document).ready(function() { 
 
\t timerSet(9,12); 
 
\t timerRun(); 
 
}); 
 

 
function timerReset() { 
 
\t alert("workin"); 
 
} 
 

 
function timerSet(inputMinutes, inputSeconds) { 
 
\t minutes = inputMinutes; \t 
 
\t seconds = inputSeconds; \t 
 
\t finalTimeInSeconds = minutes*60 + seconds; //finalTimeInSeconds is the time it takes for the timer to be 00:00 in seconds. 
 
\t timerPrint(); 
 
} 
 

 
function timerAdd(inputMinutes, inputSeconds) { 
 
\t alert("workin"); 
 
} 
 

 
function timerSubtract(inputMinutes, inputSeconds) { 
 
\t setTimeout(function() { 
 
\t \t if(minutes > 0 && seconds == 0) { 
 
\t \t \t minutes--; 
 
\t \t \t seconds = 59; 
 
\t \t } else { 
 
\t \t \t seconds--; 
 
\t \t } 
 
\t \t timerPrint(); 
 
\t }, 1000); 
 
} 
 

 
function timerRun() { 
 
\t timerSubtract(); 
 
} 
 

 
function timerStop() { 
 
\t alert("workin"); 
 
} 
 

 
function timerPrint() { 
 
\t displayMinutes = (minutes.toString().length == 2) ? minutes : "0" + minutes; \t //ternary operator: adds a zero to the beggining 
 
\t displaySeconds = (seconds.toString().length == 2) ? seconds : "0" + seconds; \t //of the number if it has only one caracter. 
 
\t $("#timerText").text(displayMinutes + ":" + displaySeconds); 
 
} 
 

 
function totalTime() { 
 
\t var totalTimeInSeconds = minutes*60 + seconds; 
 

 
\t return totalTimeInSeconds; //totalTimeInSeconds is the time that the timer now displays in seconds. 
 
} 
 

 
</script> 
 

 
</head> 
 

 
<body> 
 

 
<div id="timerText">00:00</div> 
 

 
</body> 
 

 
</html>

所以这是我的问题:在timerRun()功能,我想牛逼他timerSubtract()函数重复,而totalTime() > 0,但页面只是崩溃,如果我使用一个while循环。它为什么这样做?我不认为这是一个无限循环。我能做些什么来想要我想要的?

感谢无论谁回答! :-)

+0

这看起来像功课... – Maess

+0

@Maess那么,如果它是... –

+1

你想如何添加while循环展望。它会崩溃的原因是因为没有'setTimeout'将等待1秒钟调用'timerSubract()'你做什么你叫它'timerSubtract()'所有的时间,没有任何停顿之前。确保你的while循环在调用'timerSubtract()'之前等待一会儿。可悲的是,你如何使用while循环的逻辑失败。 – AndersRehn

回答

6

问题是timerSubtract它使用的是setTimeout

每当您使用setTimeout时,您都可以想象它将在稍后放置该功能。然后,只要程序没有做任何事情(如果足够的时间已经过去),那么它会运行下一个被搁置的功能。通过运行while循环,您永远不会给运行时机会运行使用setTimeout预留的功能。

解决这一点的同时仍使用setTimeout将做这样的事情的一种方式:

function timerRun() { 
    if (minutes > 0 && seconds == 0) { 
     minutes--; 
     seconds = 59; 
    } else { 
     seconds--; 
    } 
    timerPrint(); 
    // Queue up timerRun to run again in 1 second 
    setTimeout(timerRun, 1000); 
} 
timerRun(); 
+0

你的答案有用,非常感谢! –

+0

如何停止计时器?然后再恢复它? –

+0

有关如何启动和停止计时器的例子,我建议[检查MDN页的setTimeout(https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout)。 –

2

而不是使用setTimeout的,为什么不使用的setInterval:

http://jsfiddle.net/ouL1yzpq/

setInterval(function() { 
    if(minutes > 0 && seconds == 0) { 
     minutes--; 
     seconds = 59; 
    } else { 
     seconds--; 
    } 
    timerPrint(); 
}, 1000); 

请查看如何停止计时器:how to stop "setInterval"

0

timerSubtract函数不会更改变量minutesseconds。它只会启动一个超时,以后会改变变量。

如果您在循环中运行函数,等待变量发生变化,那么变量永远不会发生任何变化。 JavaScript是单线程的,因此您必须退出该功能并将控件返回给浏览器才能执行超时。在循环运行时,超时事件将不会被处理。

如果JavaScript本来是多线程的,那么你的循环将启动数百万次超时,直到第二次超时,并且超时开始执行。由于您不会每秒发生一次超时但尽可能快的速度,因此计时器会在当时立即倒数至数百万。

而不是启动大量的超时,请使用timerSubtract中的一个setInterval,并且只调用一次函数。间隔倒计时每秒一次,我怀疑是你希望你的定时器工作:

function timerSubtract(inputMinutes, inputSeconds) { 
    setInterval(function() { 
     if(minutes > 0 && seconds == 0) { 
      minutes--; 
      seconds = 59; 
     } else { 
      seconds--; 
     } 
     timerPrint(); 
    }, 1000); 
} 

function timerRun() { 
    timerSubtract(); 
} 
0

乔纳森发布后,您可以使用的setInterval。我想补充clearInterval(),以他的回答停在0(就是你想要什么?) 像这样:

function timerSubtract(inputMinutes, inputSeconds) { 
    var countdown = setInterval(function() { 
     if(minutes > 0 && seconds == 0) { 
      minutes--; 
      seconds = 59; 
     } else { 
      seconds--; 
     } 

     if(totalTime() <= 0){ 
      clearInterval(countdown); 
      // timer is done, do something interesting.. 
     } 

     timerPrint(); 
    }, 1000); 
} 

而且,在很长一段时间的计时器可能是不准确的。如果你想更好的精度,在启动定时器读取的实际当前系统时间,然后在每个时间间隔,再次读取实际时间和减去两个值(当前 - 开始)来获得实际经过的时间。然后从最初的总数中减去经过的时间以获得剩余的当前时间。