-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
120 lines (111 loc) · 2.96 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const focusButton = document.getElementById("focus");
const buttons = document.querySelectorAll(".btn");
const shortBreakButton = document.getElementById("shortbreak");
const longBreakButton = document.getElementById("longbreak");
const startBtn = document.getElementById("btn-start");
const reset = document.getElementById("btn-reset");
const pause = document.getElementById("btn-pause");
const time = document.getElementById("time");
let set;
let active = "focus";
let count = 59;
let paused = true;
let minCount = 39;
time.textContent = `${minCount + 1}:00`;
const appendZero = (value) => {
value = value < 10 ? `0${value}` : value;
return value;
};
reset.addEventListener(
"click",
(resetTime = () => {
pauseTimer();
switch (active) {
case "long":
minCount = 14;
break;
case "short":
minCount = 4;
break;
default:
minCount = 39;
break;
}
count = 59;
time.textContent = `${minCount + 1}:00`;
})
);
const removeFocus = () => {
buttons.forEach((btn) => {
btn.classList.remove("btn-focus");
});
};
focusButton.addEventListener("click", () => {
active = "focus";
removeFocus();
focusButton.classList.add("btn-focus");
pauseTimer();
minCount = 39;
count = 59;
time.textContent = `${minCount + 1}:00`;
});
shortBreakButton.addEventListener("click", () => {
active = "short";
removeFocus();
shortBreakButton.classList.add("btn-focus");
pauseTimer();
minCount = 4;
count = 59;
time.textContent = `${appendZero(minCount + 1)}:00`;
});
longBreakButton.addEventListener("click", () => {
active = "long";
removeFocus();
longBreakButton.classList.add("btn-focus");
pauseTimer();
minCount = 14;
count = 59;
time.textContent = `${minCount + 1}:00`;
});
pause.addEventListener(
"click",
(pauseTimer = () => {
paused = true;
clearInterval(set);
startBtn.classList.remove("hide");
pause.classList.remove("show");
reset.classList.remove("show");
})
);
startBtn.addEventListener("click", () => {
reset.classList.add("show");
pause.classList.add("show");
startBtn.classList.add("hide");
startBtn.classList.remove("show");
if (paused) {
paused = false;
time.textContent = `${appendZero(minCount)}:${appendZero(count)}`;
set = setInterval(() => {
count--;
time.textContent = `${appendZero(minCount)}:${appendZero(count)}`;
if (count == 0) {
if (minCount != 0) {
minCount--;
count = 60;
} else {
clearInterval(set);
resetTime(); // Reset the timer when it reaches zero
}
}
}, 1000);
}
});
var icon = document.getElementById("icon");
icon.onclick = function () {
document.body.classList.toggle("dark-theme");
if (document.body.classList.contains("dark-theme")) {
icon.src = "sun.png";
} else {
icon.src = "moon.png";
}
};