forked from C-Bandstra/beyonce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.js
39 lines (35 loc) · 967 Bytes
/
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
class Timer {
constructor() {
this.startTime = null;
this.stopTime = null;
this.topTimes = JSON.parse(localStorage.getItem('allScores')) || [];
this.totalSeconds = 0;
this.minutes = 0;
this.seconds = 0;
this.started = false;
}
translateMin() {
var totalSeconds = Math.round((this.stopTime - this.startTime) / 1000);
this.minutes = Math.floor(totalSeconds / 60);
this.seconds = Math.round(totalSeconds % 60);
this.topTimes.push(totalSeconds);
this.topTimes.sort(function (a, b) {
return a - b;
});
gameOver(this.minutes, this.seconds);
this.saveToLocalStorage(this.topTimes);
}
start() {
this.startTime = Date.now();
this.started = true;
}
stop() {
this.stopTime = Date.now();
this.translateMin();
this.started = false;
}
saveToLocalStorage() {
var stringedArray = JSON.stringify(this.topTimes);
localStorage.setItem('allScores', stringedArray);
}
}