-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
94 lines (77 loc) · 2.56 KB
/
index.js
File metadata and controls
94 lines (77 loc) · 2.56 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
class Timer {
constructor(root) {
root.innerHTML = Timer.getHTML();
this.el = {
minutes: root.querySelector(".timer__part--minutes"),
seconds: root.querySelector(".timer__part--seconds"),
control: root.querySelector(".timer__btn--control"),
reset: root.querySelector(".timer__btn--reset")
};
this.interval = null;
this.remainingSeconds = 0;
this.el.control.addEventListener("click", () => {
if (this.interval === null) {
this.start();
} else {
this.stop();
}
});
this.el.reset.addEventListener("click", () => {
const inputMinutes = prompt("Enter number of minutes:");
if (inputMinutes < 60) {
this.stop();
this.remainingSeconds = inputMinutes * 60;
this.updateInterfaceTime();
}
});
}
updateInterfaceTime() {
const minutes = Math.floor(this.remainingSeconds / 60);
const seconds = this.remainingSeconds % 60;
this.el.minutes.textContent = minutes.toString().padStart(2, "0");
this.el.seconds.textContent = seconds.toString().padStart(2, "0");
}
updateInterfaceControls() {
if (this.interval === null) {
this.el.control.innerHTML = `<span class="material-icons">play_arrow</span>`;
this.el.control.classList.add("timer__btn--start");
this.el.control.classList.remove("timer__btn--stop");
} else {
this.el.control.innerHTML = `<span class="material-icons">pause</span>`;
this.el.control.classList.add("timer__btn--stop");
this.el.control.classList.remove("timer__btn--start");
}
}
start() {
if (this.remainingSeconds === 0) return;
this.interval = setInterval(() => {
this.remainingSeconds--;
this.updateInterfaceTime();
if (this.remainingSeconds === 0) {
this.stop();
}
}, 1000);
this.updateInterfaceControls();
}
stop() {
clearInterval(this.interval);
this.interval = null;
this.updateInterfaceControls();
}
static getHTML() {
return `
<span class="timer__part timer__part--minutes">00</span>
<span class="timer__part">:</span>
<span class="timer__part timer__part--seconds">00</span>
<button type="button" class="timer__btn timer__btn--control timer__btn--start">
<span class="material-icons">play_arrow</span>
</button>
<button type="button" class="timer__btn timer__btn--reset">
<span class="material-icons">timer</span>
</button>
`;
}
}
new Timer(
document.querySelector(".timer")
);