-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdown.js
More file actions
27 lines (21 loc) · 879 Bytes
/
Copy pathcountdown.js
File metadata and controls
27 lines (21 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function startCountdown(duration) {
let time = duration; // time in seconds
const countdownInterval = setInterval(() => {
// Calculate hours, minutes, and seconds
const hours = Math.floor(time / 3600);
const minutes = Math.floor((time % 3600) / 60);
const seconds = time % 60;
// Format the time as HH:MM:SS
const formattedTime = `${String(hours).padStart(2, "0")}:${String(
minutes
).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
console.log(formattedTime); // Display the countdown time (or update the UI)
if (time <= 0) {
clearInterval(countdownInterval); // Stop the countdown when time reaches zero
console.log("Countdown finished!");
}
time--; // Decrease time by 1 second
}, 1000); // Repeat every 1000 ms (1 second)
}
// Start countdown for 10 minutes (600 seconds)
startCountdown(600);