I'm working on a timer, and after some answers that I got in this forum it has all been working smoothly. This is what it looks like right now: (just so that you get the idea)
My code (please note that this is a work in progress so some stuff isn't quite finished; basically every function that has a alert("workin");
is still not being used.
<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() {
timerSet(9,12);
timerRun();
});
function timerReset() {
alert("workin");
}
function timerSet(inputMinutes, inputSeconds) {
minutes = inputMinutes;
seconds = inputSeconds;
finalTimeInSeconds = minutes*60 + seconds; //finalTimeInSeconds is the time it takes for the timer to be 00:00 in seconds.
timerPrint();
}
function timerAdd(inputMinutes, inputSeconds) {
alert("workin");
}
function timerSubtract(inputMinutes, inputSeconds) {
setTimeout(function () {
if(minutes > 0 && seconds == 0) {
minutes--;
seconds = 59;
} else {
seconds--;
}
timerPrint();
}, 1000);
}
function timerRun() {
timerSubtract();
}
function timerStop() {
alert("workin");
}
function timerPrint() {
displayMinutes = (minutes.toString().length == 2) ? minutes : "0" + minutes; //ternary operator: adds a zero to the beggining
displaySeconds = (seconds.toString().length == 2) ? seconds : "0" + seconds; //of the number if it has only one caracter.
$("#timerText").text(displayMinutes + ":" + displaySeconds);
}
function totalTime() {
var totalTimeInSeconds = minutes*60 + seconds;
return totalTimeInSeconds; //totalTimeInSeconds is the time that the timer now displays in seconds.
}
</script>
</head>
<body>
<div id="timerText">00:00</div>
</body>
</html>
So this is my problem: In the timerRun()
function, I want the timerSubtract()
function to repeat while totalTime() > 0
, but the page just crashes if I use a while loop. Why does it do that? I don't think it's an infinit loop. What can I do to do want I want?
Thanks to whoever answers! :-)