-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.ts
54 lines (44 loc) · 1.35 KB
/
script.ts
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
50
51
52
53
54
let timer: number | undefined;
let countdown: number | undefined;
const timerDisplay = document.getElementById("timer") as HTMLElement;
const startBtn = document.getElementById("start-btn") as HTMLButtonElement;
const resetBtn = document.getElementById("reset-btn") as HTMLButtonElement;
const stopBtn = document.getElementById("stop-btn") as HTMLButtonElement;
const startTime = 25 * 60;
timer = startTime;
function updateTimerDisplay(seconds: number) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
timerDisplay.textContent = `${minutes}:${
remainingSeconds < 10 ? "0" : ""
}${remainingSeconds}`;
}
function startTimer() {
if (countdown) return; // Prevent multiple intervals
countdown = setInterval(() => {
timer--;
updateTimerDisplay(timer);
if (timer <= 0) {
clearInterval(countdown);
countdown = undefined;
timer = startTime;
alert("Time's up!");
}
}, 1000);
}
function stopTimer() {
if (countdown !== undefined) {
clearInterval(countdown);
countdown = undefined;
}
}
function resetTimer() {
clearInterval(countdown);
countdown = undefined;
timer = startTime;
updateTimerDisplay(timer);
}
startBtn.addEventListener("click", startTimer);
resetBtn.addEventListener("click", resetTimer);
stopBtn.addEventListener("click", stopTimer);
updateTimerDisplay(timer);