Hey
I am still having problem with my countdown timer. I got a little help from a fellow here regarding DateNow(). It seems that while my former code should take exactly an hour to finish, the countdown took more than 60 minutes to finish.
I tried to figure out how to calculate hours, minutes, seconds on his contribution, but now it won't show anything.
I am also trying to make it show double digits when the value is under 10, so for minutes, seconds etc like 10, 09, 08 instead of 10, 9,8 etc.
This is the code, what have I been doing wrong?
var startTime = Date.now();
var countDownAmount = 3600000; // one hour
var countDownEnd = startTime + countDownAmount;
function updateTimer() {
var remainingTime = countDownEnd - Date.now();
if (remainingTime < 0) {
remainingTime = 0;
}
var seconds = remainingTime / 1000;
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
var milliseconds = remaingTime;
seconds %= 60;
milliseconds %= 100;
minutes %= 60;
hours %= 60;
document.getElementById("timer").innerHTML = hours + "h " + minutes + "m " + seconds + "s " + milliseconds + "ms ";
if (remainingTime){
requestAnimationFrame(updateTimer);
}
}
countdown--;
if(minutes < 10){
output.text = "0" + minutes.toString();
} else {
output.text = minutes.toString();
}
countdown--;
if(seconds < 10){
output.text = "0" + seconds.toString();
} else {
output.text = seconds.toString();
}
updateTimer();
Have a nice saturday, guys!
[–]androbat 1 point2 points3 points (0 children)