-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathTimer.js
More file actions
41 lines (33 loc) · 871 Bytes
/
Timer.js
File metadata and controls
41 lines (33 loc) · 871 Bytes
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
const DEFAULT_TIMER = 60;
class Timer {
constructor({ gameSession}) {
this.seconds = 0;
this.intervalId = null
this._onTimeExpired = () => null
this.gameSession = gameSession;
}
start() {
this.intervalId = setInterval(() => {
this.seconds += 1;
this.gameSession.emitGameEvent({
message: this.secondsLeft(),
eventName: 'time',
})
if (this.seconds >= DEFAULT_TIMER) {
this.stop();
this._onTimeExpired(this.gameSession);
}
}, 1000)
}
stop() {
this.seconds = 0;
clearInterval(this.intervalId);
}
secondsLeft() {
return DEFAULT_TIMER - this.seconds;
}
onTimeExpired(fn) {
this._onTimeExpired = fn
}
}
module.exports = Timer