Skip to content

Commit f80a942

Browse files
Merge pull request #850 from Rohit12012007/patch-14
Update script.js
2 parents 0be1b24 + af43c4e commit f80a942

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

Countdown_Timer/script.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,32 @@ const hoursEl = document.getElementById("hours");
33
const minsEl = document.getElementById("mins");
44
const secondsEl = document.getElementById("seconds");
55

6-
const Diwali = "4 Nov 2021";
6+
// Set the upcoming Diwali date
7+
const Diwali = "4 Nov 2024";
78

89
function countdown() {
910
const diwaliDate = new Date(Diwali);
1011
const currentDate = new Date();
1112

13+
// Calculate total seconds until Diwali
1214
const totalSeconds = (diwaliDate - currentDate) / 1000;
1315

16+
// If the countdown has reached zero or passed
17+
if (totalSeconds < 0) {
18+
// Display "Celebration!" if the date has passed
19+
daysEl.innerHTML = "0";
20+
hoursEl.innerHTML = "00";
21+
minsEl.innerHTML = "00";
22+
secondsEl.innerHTML = "00";
23+
return; // Stop the countdown
24+
}
25+
1426
const days = Math.floor(totalSeconds / 3600 / 24);
1527
const hours = Math.floor(totalSeconds / 3600) % 24;
1628
const mins = Math.floor(totalSeconds / 60) % 60;
1729
const seconds = Math.floor(totalSeconds) % 60;
1830

31+
// Update the HTML elements with the calculated values
1932
daysEl.innerHTML = days;
2033
hoursEl.innerHTML = formatTime(hours);
2134
minsEl.innerHTML = formatTime(mins);
@@ -26,7 +39,8 @@ function formatTime(time) {
2639
return time < 10 ? `0${time}` : time;
2740
}
2841

29-
// initial call
42+
// Initial call to update the countdown immediately
3043
countdown();
3144

32-
setInterval(countdown, 1000);
45+
// Set an interval to update the countdown every second
46+
setInterval(countdown, 1000);

0 commit comments

Comments
 (0)