-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchessTimer.js
More file actions
60 lines (52 loc) · 1.01 KB
/
chessTimer.js
File metadata and controls
60 lines (52 loc) · 1.01 KB
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
class ChessTimer {
selector;
minutes;
seconds;
increment;
timer;
ended;
constructor(selector, minutes, increment) {
this.selector = selector;
this.minutes = minutes;
this.seconds = 0;
this.increment = increment;
this.timer = null;
this.ended = false;
}
startTimer() {
var self = this;
this.timer = setInterval(function(){self.decrementTimer();}, 1000);
}
pauseTimer() {
clearInterval(this.timer);
}
timerEnd() {
this.pauseTimer();
this.ended = true;
boardEndTimer(this.selector);
}
decrementTimer() {
this.seconds--;
if(this.seconds < 0) {
this.minutes--;
this.seconds = 59;
}
if(this.minutes == 0 && this.seconds == 0) {
this.timerEnd();
}
this.drawTimer();
}
getTimerText() {
return this.minutes + ":" + this.seconds.toString().padStart(2, "0");
}
drawTimer() {
$(this.selector).text(this.getTimerText());
}
incrementTimer() {
this.seconds += parseInt(this.increment);
while(this.seconds >= 60) {
this.seconds -= 60;
this.minutes++;
}
}
}