-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path100-projects-011-countdown-timer2.js
More file actions
30 lines (25 loc) · 1.06 KB
/
100-projects-011-countdown-timer2.js
File metadata and controls
30 lines (25 loc) · 1.06 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
'use strict'
let countdownInterval;
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
let 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);
}