-
Notifications
You must be signed in to change notification settings - Fork 1
/
Timer.js
53 lines (39 loc) · 1.1 KB
/
Timer.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
const { dialog } = require('electron');
class Timer {
constructor(updateView) {
this.updateView = updateView;
this.started = false;
this.intervalId = null;
this.secondsToGo = 20 * 60;
this.secondsLeft = this.secondsToGo;
}
toggle() {
if (!this.started) this.start();
else this.pause();
}
start(minutes) {
if (this.started) this.pause();
if (minutes) this.secondsToGo = minutes * 60;
this.started = true;
this.secondsLeft = this.secondsToGo;
this.updateView();
let startTime = Date.now();
this.intervalId = setInterval(() => {
const spent = Math.round((Date.now() - startTime) / 1000); // seconds
this.secondsLeft = this.secondsToGo - spent;
this.updateView();
if (this.secondsLeft <= 0) {
this.pause();
this.secondsToGo = 20 * 60;
dialog.showMessageBox({ message: "Time to take a break!", buttons: [] });
}
}, 1000);
}
pause() {
this.started = false;
this.secondsToGo = this.secondsLeft;
clearInterval(this.intervalId);
this.updateView();
}
}
module.exports = Timer;