Here is my JS fiddle:
https://jsfiddle.net/apasric4/xkzwqr1m/1/
My program is supposed to start a countdown timer that calculates the time remaining in days, hours, seconds, etc from today till the the date that the user picks in the input field.
While the program updates the HTML with the correct time remaining, the problem starts when I try to update countdown timer every second. It returns back NaN values for some reason. Why is that??
Here's my JS:
const input = document.querySelector('input')
let timeInterval;
let timeStop;
input.addEventListener('change', (e) => {
e.preventDefault()
timeStop = true;
endTime = Date.parse(e.target.value)
updateHTML(endTime)
})
function updateHTML(endTime) {
let time = calculateTimeDiff(endTime)
if (time.total <= 0) {
timeStop = false;
}
for (let pro in time) {
let el = document.querySelector(`.${pro}`)
if (el) {
el.innerHTML = time[pro];
}
}
updateCounter();
}
function updateCounter () {
if (timeStop) {
timeInterval = setInterval(updateHTML, 1000);
} else {
clearInterval(timeInterval);
}
}
//returning time remaining till date in object form
function calculateTimeDiff(endTime) {
let start = Date.now();
let end = endTime;
let t = (end-start);
let seconds = Math.floor((t / 1000) % 60);
let minutes = Math.floor((t / 1000 / 60) % 60);
let hours = Math.floor((t / (1000 * 60 * 60)) % 24);
let days = Math.floor(t / (1000 * 60 * 60 * 24));
return {
total: t,
days: days,
hours: hours,
minutes: minutes,
seconds: seconds
}
}