[go: up one dir, main page]

Menu

[r5]: / modules / quiz / timer.js  Maximize  Restore  History

Download this file

53 lines (43 with data), 1.3 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
function JSTimer(containerId, hr, min, sec) {
this.containerId = containerId;
this.container = document.getElementById(containerId);
this.spanObjectId = containerId + '_jsTimer';
var spanObj = document.createElement('span');
spanObj.id = this.spanObjectId;
this.container.appendChild(spanObj);
this.hour = hr;
this.minute = min;
this.second = sec;
this.timerId = 0;
this.tickHandlers = new Array();
this.init();
}
JSTimer.prototype = {
init: function() {
var self = this;
this.timerId = setInterval(function() { self.tick(); }, 1000);
},
tick: function() {
if (++this.second == 60) {
this.second = 0;
if (++this.minute == 60) {
++this.hour;
this.minute = 0;
}
}
var curTime = this.format(this.hour, this.minute, this.second);
if (this.tickHandlers[curTime] != null)
this.tickHandlers[curTime](this.hour, this.minute, this.second);
document.getElementById(this.spanObjectId).innerHTML = curTime;
},
stop: function() {
clearInterval(this.timerId);
this.timerId = 0;
},
format: function(h, m, s) {
return (h < 10 ? '0' + h : h) + ':' + (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s);
},
addTickHandler: function(hr, min, sec, func) {
this.tickHandlers[this.format(hr, min, sec)] = func;
}
};