I am trying to create a very simple JavaScript count down timer, I have a problem when displaying "minute" with string "0" when it has only 1 digit, it adds "0" every second while "seconds" do not add more "0", only one "0" when it displays 1 numerical digit, how do I display only one "0" to a minute?
Regards.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>count down test</title>
</head>
<body>
<p id="demo"></p>
<script>
var seconds = 10; //10 sec just to test, originally 60
var minutes = 5; //10 min just to test, originally 59
var hour = 1; //can be set to 48 hours for 2days, 72 hours for 3days
//function to display time
function count() {
if(hour == 0 && minutes == 0 && seconds == 0) {
clearInterval();
}
//reset seconds when reached 0
if(seconds == 0) {
seconds = 10; //originally 60
minutes--;
}
if(minutes == 0) {
minutes = 10; //originally 59
if(hour != 0) {
hour--;
}
}
//-1sec every second
seconds--;
//if less than 10 sec add "string" character 0 to first digit
if(seconds < 10) {
seconds = "0" + seconds;
}
//if less than 10 min add "string" character 0 to first digit
if(minutes < 10) {
minutes = "0" + minutes;
}
document.getElementById("demo").innerHTML = hour + " : " + minutes + " : " + seconds;
}
count();
setInterval(function(){
count();
}, 1000);
</script>
</body>
</html>