-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
105 lines (88 loc) · 1.98 KB
/
app.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
import Alpine from "alpinejs";
Alpine.data("app", () => ({
initTime: 25,
timeScale: 5,
currTime: 25,
timeArea: "",
running: false,
paused: true,
timerID: "",
totalSeconds: 0,
init() {
this.running = false;
this.timeArea = `${this.currTime}:00`;
this.updataProgressBar(100);
},
reset() {
this.paused = true;
this.stopTimer();
this.init();
},
addTime() {
this.currTime += this.timeScale;
this.init();
},
reduceTime() {
this.currTime =
this.currTime - this.timeScale > 0 ? this.currTime - this.timeScale : 1;
this.init();
},
setTime() {
this.init();
},
run() {
if (!this.running) {
this.paused = false;
this.start();
} else {
if (!this.paused) {
this.pauseTimer();
} else {
this.resumeTimer();
}
}
},
start() {
let barScale;
if (!this.running) {
this.running = true;
this.totalSeconds = this.currTime * 60;
barScale = 100 / this.totalSeconds;
}
this.timerID = setInterval(() => {
if (this.totalSeconds > 1) {
this.totalSeconds--;
this.updataTimer(this.totalSeconds);
const percentage =
100 - barScale * (this.currTime * 60 - this.totalSeconds);
this.updataProgressBar(percentage);
} else {
this.stopTimer();
this.updataProgressBar(0);
}
}, 1000);
},
updataTimer(Second) {
console.log("Second", Second);
const min = String(Math.floor(Second / 60)).padStart(2, "0");
const sec = String(Math.floor(Second % 60)).padStart(2, "0");
this.timeArea = `${min}:${sec}`;
},
updataProgressBar(percentage) {
this.$refs.progressBar.style.width = percentage + "%";
},
resumeTimer() {
this.start();
this.paused = false;
},
pauseTimer() {
clearInterval(this.timerID);
this.paused = true;
},
stopTimer() {
clearInterval(this.timerID);
this.updataTimer(0);
this.running = false;
},
}));
Alpine.start();