-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
98 lines (86 loc) · 2.24 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
// ===================== PER ATSKIRAS FUNKCJAS =====================
// let min = 0;
// let sek = 0;
// let setTiming;
// const startTime = document.querySelector(".time");
// startTime.textContent = `00 : 00`;
// const timing = () => {
// sek++;
// if (sek === 59) {
// min++;
// sek = 0;
// }
// const time = document.querySelector(".time");
// time.textContent = `${min} : ${sek}`;
// };
// const increment = () => {
// if (!setTiming) {
// setTiming = setInterval(timing, 1000);
// }
// };
// const stop = () => {
// clearInterval(setTiming);
// setTiming = null;
// };
// const reset = () => {
// clearInterval(setTiming);
// setTiming = null;
// min = 0;
// sek = 0;
// const time = document.querySelector(".time");
// time.textContent = `${min} : ${sek}`;
// };
// ===================== SU KLASE BUDAS =====================
class Laikmatis {
constructor(min, sek, setTiming) {
this.min = min;
this.sek = sek;
this.setTiming = setTiming;
}
timing() {
this.sek++;
if (this.sek === 59) {
this.min++;
this.sek = 0;
}
if (this.min === 10) {
alert("Time out");
this.stop();
}
const time = document.querySelector(".time");
time.textContent = `${this.min > 9 ? this.min : `0${this.min}`} : ${
this.sek < 10 ? `0${this.sek}` : this.sek
}`;
}
stop() {
clearInterval(this.setTiming);
this.setTiming = null;
}
reset() {
clearInterval(this.setTiming);
this.setTiming = null;
this.min = 0;
this.sek = 0;
const time = document.querySelector(".time");
time.textContent = `0${this.min} : 0${this.sek}`;
}
}
const laikmatis = new Laikmatis(0, 0, null);
const startTime = document.querySelector(".time");
startTime.textContent = `00 : 00`;
const btnStart = document.querySelector(".btn-start");
btnStart.addEventListener("click", function () {
if (!laikmatis.setTiming) {
laikmatis.setTiming = setInterval(function () {
laikmatis.timing();
}, 1000);
}
});
const btnStop = document.querySelector(".btn-stop");
btnStop.addEventListener("click", function () {
laikmatis.stop();
});
const btnReset = document.querySelector(".btn-reset");
btnReset.addEventListener("click", function () {
laikmatis.reset();
});