-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
55 lines (47 loc) · 1.29 KB
/
script.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
50
51
52
53
54
55
var interval;
var minutes = 25;
var seconds = 0;
var timerStarted = false;
function startTimer() {
if (!timerStarted) {
interval = setInterval(updateTimer, 1000);
timerStarted = true;
document.getElementById("startBtn").disabled = true;
}
}
function stopTimer() {
clearInterval(interval);
timerStarted = false;
document.getElementById("startBtn").disabled = false;
}
function resetTimer() {
clearInterval(interval);
minutes = 25;
seconds = 0;
updateDisplay();
timerStarted = false;
document.getElementById("startBtn").disabled = false;
}
function updateTimer() {
if (minutes === 0 && seconds === 0) {
stopTimer();
alert("Time's up!");
return;
}
if (seconds === 0) {
minutes--;
seconds = 59;
} else {
seconds--;
}
updateDisplay();
}
function updateDisplay() {
var timerElement = document.getElementById("timer");
var formattedMinutes = minutes < 10 ? "0" + minutes : minutes;
var formattedSeconds = seconds < 10 ? "0" + seconds : seconds;
timerElement.innerHTML = formattedMinutes + ":" + formattedSeconds;
}
document.getElementById("startBtn").addEventListener("click", startTimer);
document.getElementById("stopBtn").addEventListener("click", stopTimer);
document.getElementById("resetBtn").addEventListener("click", resetTimer);