我正在一个计时器,并在一些答案后,我得到了在这个论坛它一直工作顺利。这是什么样子,现在(只是让你的想法)无限循环在没有一个不透明的原因
我的代码(请注意,这是在这样一些东西没有完全结束正在进行的工作,基本上每仍然没有被使用,有一个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循环。它为什么这样做?我不认为这是一个无限循环。我能做些什么来想要我想要的?
感谢无论谁回答! :-)
这看起来像功课... – Maess
@Maess那么,如果它是... –
你想如何添加while循环展望。它会崩溃的原因是因为没有'setTimeout'将等待1秒钟调用'timerSubract()'你做什么你叫它'timerSubtract()'所有的时间,没有任何停顿之前。确保你的while循环在调用'timerSubtract()'之前等待一会儿。可悲的是,你如何使用while循环的逻辑失败。 – AndersRehn