forked from lkrryba/weekly-web-dev-challenge-poll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallengeCountdown.js
24 lines (20 loc) · 900 Bytes
/
challengeCountdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
export const countdown = function (date) {
const challengeCountdownEl = document.getElementById(
"challenge-countdown-display"
);
const cd = setInterval(function () {
const currentDate = new Date();
const diffInMiliseconds = date - currentDate;
const diffInSeconds = Math.trunc(diffInMiliseconds / 1000);
const daysLeft = Math.trunc(diffInSeconds / 3600 / 24);
const hoursLeft = Math.trunc(diffInSeconds / 3600) % 24;
const minutesLeft = Math.trunc(diffInSeconds / 60) % 60;
const secondsLeft = Math.trunc(diffInSeconds) % 60;
const finalString = `${daysLeft}d ${hoursLeft}h ${minutesLeft}m ${secondsLeft}s`;
challengeCountdownEl.innerHTML = finalString;
if (diffInMiliseconds < 0) {
challengeCountdownEl.innerHTML = "ENDED";
clearInterval(cd);
}
}, 1000);
};