-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
49 lines (38 loc) · 1.21 KB
/
popup.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
let timer;
let timeLeft;
function startTimer() {
const hours = parseInt(document.getElementById('hours').value) || 0;
const minutes = parseInt(document.getElementById('minutes').value) || 0;
const seconds = parseInt(document.getElementById('seconds').value) || 0;
timeLeft = hours * 3600 + minutes * 60 + seconds;
if (timeLeft <= 0) {
alert('Please enter a valid time');
return;
}
updateTimerDisplay();
timer = setInterval(() => {
timeLeft--;
updateTimerDisplay();
if (timeLeft <= 0) {
clearInterval(timer);
alert('Time is up!');
}
}, 1000);
}
function stopTimer() {
clearInterval(timer);
}
function updateTimerDisplay() {
const hours = Math.floor(timeLeft / 3600);
const minutes = Math.floor((timeLeft % 3600) / 60);
const seconds = timeLeft % 60;
document.getElementById('timer').textContent =
`${padZero(hours)}:${padZero(minutes)}:${padZero(seconds)}`;
}
function padZero(num) {
return num.toString().padStart(2, '0');
}
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('startTimer').addEventListener('click', startTimer);
document.getElementById('stopTimer').addEventListener('click', stopTimer);
});