-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path100-projects-012-countdown-timer-up2.js
More file actions
46 lines (39 loc) · 1.46 KB
/
100-projects-012-countdown-timer-up2.js
File metadata and controls
46 lines (39 loc) · 1.46 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"use strict"
let countdownInterval;
let totalSeconds;
function startCountdown() {
let hours = parseInt(document.getElementById('hours').value);
let minutes = parseInt(document.getElementById('minutes').value);
let seconds = parseInt(document.getElementById('seconds').value);
//Convert all to seconds
totalSeconds = hours * 3600 + minutes * 60 + seconds;
//Clear any existing interval
if (countdownInterval) {
clearInterval(countdownInterval);
}
countdownInterval = setInterval(function() {
if (totalSeconds < 0) {
clearInterval(countdownInterval);
document.getElementById('countdown').innerHTML = "Countdown Finished!";
} else {
let hoursRemaining = Math.floor(totalSeconds / 3600);
let minutesRemaining = Math.floor((totalSeconds % 3600) / 60);
let secondsRemaining = totalSeconds % 60;
document.getElementById('countdown').innerHTML = hoursRemaining + "H " + minutesRemaining + "M " + secondsRemaining + "S ";
totalSeconds++
}
}, 1000);
}
function stopCountdown() {
if (countdownInterval) {
clearInterval(countdownInterval);
}
}
function resetCountdown() {
stopCountdown();
totalSeconds = 0;
document.getElementById('countdown').innerHTML = "";
document.getElementById('hours').value ="0";
document.getElementById('minutes').value = "0";
document.getElementById('seconds').value = "0";
}