-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
111 lines (94 loc) · 2.84 KB
/
Copy pathscript.js
File metadata and controls
111 lines (94 loc) · 2.84 KB
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
const POMODORO_BUTTON = document.getElementById("pomodoro");
const SHORTBREAK_BUTTON = document.getElementById("short-break");
const LONGBREAK_BUTTON = document.getElementById("long-break");
const TIMER_ELEMENT = document.getElementById("timer");
const C1_CIRCLE = document.getElementById("c1");
const C2_CIRCLE = document.getElementById("c2");
const START_BUTTON = document.getElementById("start");
const STOP_BUTTON = document.getElementById("stop");
const timers = {
pomodoro: 1 * 60,
"short-break": 5,
"long-break": 10,
};
let currentTimer = timers["pomodoro"];
let circleLeft = timers["pomodoro"];
let timeLeft = null;
let timerId = null;
function updateTimer() {
const minutes = Math.floor(timeLeft / 60)
.toString()
.padStart(2, "0");
const seconds = (timeLeft % 60).toString().padStart(2, "0");
TIMER_ELEMENT.innerText = `${minutes}:${seconds}`;
}
function setTimer(timer) {
stopTimer();
currentTimer = timers[timer];
timeLeft = timers[timer];
circleLeft = timers[timer];
updateCircle();
updateTimer();
}
function updateCircle() {
const l = ((timeLeft / circleLeft) * 100).toFixed(2);
const k = (100 - l).toFixed(2);
console.log(`timeLeft - ${timeLeft}, currentTimer|circleLeft - ${currentTimer}|${circleLeft}, l - ${l}, k - ${l}`)
C1_CIRCLE.style.strokeDasharray = `${l} ${k}`;
C2_CIRCLE.style.strokeDasharray = `${k} ${l}`;
C1_CIRCLE.style.strokeDashoffset = l;
}
POMODORO_BUTTON.addEventListener("click", setTimer.bind(null, "pomodoro"));
SHORTBREAK_BUTTON.addEventListener("click", setTimer.bind(null, "short-break"));
LONGBREAK_BUTTON.addEventListener("click", setTimer.bind(null, "long-break"));
STOP_BUTTON.addEventListener("click", stopTimer);
START_BUTTON.addEventListener("click", startTimer);
function startTimer() {
START_BUTTON.disabled = true;
STOP_BUTTON.disabled = false;
if (currentTimer !== null) {
timeLeft = currentTimer;
} else {
timeLeft = timers["pomodoro"];
}
timerId = setInterval(() => {
timeLeft--;
if (timeLeft + 1 === 0) {
clearInterval(timerId);
timeLeft = null;
START_BUTTON.disabled = false;
STOP_BUTTON.disabled = true;
chooseBreakType();
}
updateCircle();
updateTimer();
}, 1000);
}
function chooseBreakType() {
const breakTypes = Object.keys(timers).slice(1);
let breakChosen = false;
const breakType = breakTypes
.map((type) => {
if (!breakChosen && confirm(`Выберите тип перерыва: ${type}?`)) {
breakChosen = true;
return type;
}
return null;
})
.find(Boolean);
if (breakType) {
setTimer(breakType);
startTimer();
} else {
setTimer("pomodoro");
startTimer();
}
}
function stopTimer() {
clearInterval(timerId);
START_BUTTON.disabled = false;
STOP_BUTTON.disabled = true;
if (timeLeft !== null) {
currentTimer = timeLeft;
}
}