I'm making a webpage that shows the current time, and a countdown to a hardcoded time. the current time works perfect, but the countdown shows NaN instead of the numbers... there are no console errors. this is my code:
<!DOCTYPE html>
<html>
<head>
<script>
function startTime() {
var now = new Date();
var deadline = new Date();
var countdown = new Date("12:10:00");
// deadline setten moet via gui kunnen____________________________
// deadline.setHours(11);
// deadline.setMinutes(10);
// deadline.setSeconds(0);
//________________________________________________________________
var clockH = now.getHours();
var clockM = now.getMinutes();
var clockS = now.getSeconds();
// var countdownH = getCountdown(deadline).hours;
// var countdownM = getCountdown(deadline).minutes;
// var countdownS = getCountdown(deadline).seconds;
//m = checkTime(m);
//s = checkTime(s);
startClock('clock');
startCountdown('countdown', deadline);
var t = setTimeout(startTime, 500);
}
function getCountdown(deadline){
var countdownTotal = Date.parse(deadline) - Date.parse(new Date());
var countdownS = Math.floor( (countdown/1000) % 60 );
var countdownM = Math.floor( (countdown/1000/60) % 60 );
var countdownH = Math.floor( (countdown/(1000*60*60)) % 24 );
return{
'countdownTotal': countdownTotal,
'countdownH': countdownH,
'countdownM': countdownM,
'countdownS': countdownS
}
}
function startClock(id){
var clock = document.getElementById(id);
var timeInterval = setInterval(function(){
var now = new Date();
var nowH = now.getHours();
var nowM = now.getMinutes();
var nowS = now.getSeconds();
clock.innerHTML = 'hours: '+ nowH + ' minutes: ' + nowM + ' seconds: ' + nowS;
}, 1000);
}
function startCountdown(id, deadline){
var countdown = document.getElementById(id);
var timeInterval = setInterval(function(){
var t = getCountdown(deadline);
countdown.innerHTML = 'hours: '+ t.countdownH + ' minutes: ' + t.countdownM + ' seconds: ' + t.countdownS;
if(t.countdownTotal <= 0){
clearInterval(timeInterval);
}
}, 1000);
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
</script>
</head>
<body onload="startTime()">
<div id="clock"> </div><br>
<div id="countdown"> </div>
</body>
</html>