-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_FINAL.js
75 lines (64 loc) · 2.08 KB
/
app_FINAL.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
const timer = document.getElementById("timer");
const modeButtons = document.querySelector("[class=modeSelector]");
const pomodoroButton = document.getElementById("pomodoroButton");
const shortButton = document.getElementById("shortButton");
const longButton = document.getElementById("longButton");
const mainButton = document.getElementById("toggle");
let seconds = 0;
const TIMER = {
POMODORO: 25,
SHORTBREAK: 5,
LONGBREAK: 15,
};
function changeMode(e) {
mainButton.classList.replace("fa-stop", "fa-play");
mainButton.dataset.paused = "true";
for (let i = 0; i < 3; i++) {
e.path[1].children[i].classList.remove("active");
}
e.target.classList.add("active");
let mode = e.target.dataset.mode;
timer.dataset.mode = mode;
if (timer.dataset.mode === "pomodoro") {
timer.innerHTML = `${TIMER.POMODORO}:00`;
} else if (timer.dataset.mode === "short") {
timer.innerHTML = `0${TIMER.SHORTBREAK}:00`;
} else {
timer.innerHTML = `${TIMER.LONGBREAK}:00`;
}
}
function pomodoro() {
function setTimer() {
if (timer.dataset.mode === "pomodoro") {
seconds = TIMER.POMODORO * 60;
} else if (timer.dataset.mode === "short") {
seconds = TIMER.SHORTBREAK * 60;
} else {
seconds = TIMER.LONGBREAK * 60;
}
}
if (mainButton.classList.contains("fa-play")) {
mainButton.classList.replace("fa-play", "fa-stop");
mainButton.dataset.paused = "false";
setTimer();
interval = setInterval(() => {
let timeRemaining =
("0" + Math.floor(seconds / 60)).slice(-2) +
":" +
("0" + (seconds % 60)).slice(-2);
timer.innerHTML = timeRemaining;
document.title = `${timeRemaining} - ${
timer.dataset.mode === "pomodoro" ? "Work" : "Break"
}`;
if (mainButton.dataset.paused === "true" || seconds === 0) {
clearInterval(interval);
}
seconds--;
}, 1000);
} else {
mainButton.classList.replace("fa-stop", "fa-play");
mainButton.dataset.paused = "true";
}
}
mainButton.addEventListener("click", pomodoro);
modeButtons.addEventListener("click", changeMode);