-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtimer.js
33 lines (31 loc) · 922 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
function Countdown(seconds, display) {
var ticksLeft = seconds,
self = this, ticker;
self.active = true;
function getMins() {
return Math.floor(ticksLeft / 60);
}
function getSecs() {
return (ticksLeft < 60) ? ticksLeft : ((ticksLeft % 60 === 0) ? 0 : (Math.round(((ticksLeft / 60) - Math.floor(ticksLeft / 60)) * 60)));
}
function setOff() {
self.ticker = setInterval(update, 1000);
}
this.render = function() {
var minutes = getMins(),
seconds = getSecs();
display.html(('Timer: ' + minutes + ':' + ((seconds < 10) ? '0' + seconds : seconds)));
};
function update() {
if (ticksLeft >= 0) {
self.render();
ticksLeft -= 1;
} else {
self.active = false;
clearInterval(ticker);
}
}
this.fire = function () {
setOff();
};
}