-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (51 loc) · 1.11 KB
/
index.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
export default class Screensaver {
constructor(settings) {
this.settings = Object.assign({
idleTime: 5,
clearEvents: ["mousemove", "touchstart"],
onActivation: () => {
console.log("start screensaver");
},
onDeactivation: () => {
console.log("stop screensaver");
}
}, settings)
this.init()
}
init() {
this.active = false;
this.disabled = false;
this.settings.clearEvents.forEach(e => {
window.addEventListener(e, () => {
clearTimeout(this.timeout);
if (this.active) {
this.stop();
}
this.setTimeout();
});
});
this.setTimeout();
}
setTimeout() {
if (this.disabled) return;
this.timeout = setTimeout(() => {
this.start();
}, 1000 * this.settings.idleTime);
}
start() {
this.active = true;
this.settings.onActivation()
}
stop() {
this.active = false;
this.settings.onDeactivation();
}
disable() {
this.disabled = true;
clearTimeout(this.timeout);
}
enable() {
this.disabled = false;
this.setTimeout()
}
}