I want to create simple countdown project, where there is targeted date, and I have to count how many days, hours, minutes and seconds left until that date from now. When I test my code with setInterval() it shows up in console, thus it works, but why setInterval do not change time on screen in every second?
// Variables DOM
const days = document.getElementById('day')
const hours = document.getElementById('hour')
const minutes = document.getElementById('minute')
const seconds = document.getElementById('second')
const endDateDisplay = document.getElementById('endDateDisplay');
// Static DB for days and months
const daysList = [
'Monday',
'Sunday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday'
]
const monthsList = [
'January', 'February', 'March', 'April', 'May', 'June', 'Jule', 'August', 'September', 'October', 'November', 'December'
]
// Declare present and targeted date
const startDate = new Date();
const endDate = new Date('05/08/2021/03:00');
function displayScreen() {
// Difference between today and targeted time in milliseconds
let remainingTime = endDate.getTime() - startDate.getTime();
// For easier solution turning measure from ms to seconds
let measureDifferInSeconds = parseInt(remainingTime / 1000);
// Finding how much days, hours, mins and seconds left until targeted time, after sub respectively
const leftDays = measureDifferInSeconds / 60 / 60 / 24;
measureDifferInSeconds = measureDifferInSeconds - Math.floor(leftDays) * 60 * 60 * 24
const leftHours = measureDifferInSeconds / 60 / 60;
measureDifferInSeconds = measureDifferInSeconds - Math.floor(leftHours) * 60 * 60;
const leftMins = measureDifferInSeconds / 60;
measureDifferInSeconds = measureDifferInSeconds - Math.floor(leftMins) * 60;
const leftSec = measureDifferInSeconds;
// Left Time Display for zeros
if (leftDays < 10) {
days.innerHTML = "0" + Math.floor(leftDays) + ":";
} else {
days.innerHTML = Math.floor(leftDays) + ":";
}
if (leftHours < 10) {
hours.innerHTML = "0" + Math.floor(leftHours) + ":";
} else {
hours.innerHTML = Math.floor(leftHours) + ":";
} if (leftMins < 10) {
minutes.innerHTML = "0" + Math.floor(leftMins) + ":";
} else {
minutes.innerHTML = Math.floor(leftMins) + ":";
} if (leftSec < 10) {
seconds.innerHTML = "0" + Math.floor(leftSec);
} else {
seconds.innerHTML = Math.floor(leftSec);
}
// End Date display for zeros
let endHourZeroCheck = ''
let endMinZeroCheck = ''
if (endDate.getHours() < 10) {
endHourZeroCheck = '0' + endDate.getHours();
} else {
endHourZeroCheck = endDate.getHours()
}
if (endDate.getMinutes() < 10) {
endMinZeroCheck = '0' + endDate.getMinutes();
} else {
endMinZeroCheck = endDate.getMinutes()
}
endDateDisplay.innerHTML = `${daysList[endDate.getDay()]}, ${endDate.getDate()} ${monthsList[endDate.getMonth()]}, ${endDate.getFullYear()}, ${endHourZeroCheck}:${endMinZeroCheck}`
console.log('set timeout call');
}
setInterval(displayScreen, 1000)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Repeat session: Reviews</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"
integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w=="
crossorigin="anonymous" />
<!-- STYLE -->
<link rel="stylesheet" href="style/bc-ui.css">
<link rel="stylesheet" href="style/main.css" />
<!-- SCRIPT -->
<script src="scripts/app.js" defer></script>
</head>
<body>
<h2>It ends on <span id='endDateDisplay'></span> </h2>
<hr>
<div class="wrapper">
<h2 id="day">00:</h2>
<h2 id='hour'>00:</h2>
<h2 id='minute'>00:</h2>
<h2 id="second">00</h2>
</div>
</body>
</html>