Hourglass.js
2.19 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
61
62
63
64
65
66
67
68
69
70
71
72
(function(window){
var Timer = {
options: {
seconds: 1, // 秒数
timer: null, // 计时器
dom: null, // 存入dom节点
status: 1 // 计时状态 1: 计时中 0: 暂停中
},
init: function(options){
var _this = this;
this.options.dom = document.getElementById("timer");
this.startTimer();
this.options = $.extend(this.options, options);
},
getSeconds: function(){
var seconds = this.options.seconds;
return seconds
},
formatTime: function(secs){
var _dom = Timer.options.dom;
var time,
hour = 0,
min = 0,
seconds = secs
if (seconds > 60) {
min = parseInt(seconds / 60);
seconds = parseInt(seconds % 60);
if(min > 60){
hour = parseInt(min / 60);
min = parseInt(min % 60);
}
}
hour = hour < 10 ? ('0' + hour) : hour;
min = min < 10 ? ('0' + min) : min;
seconds = seconds < 10 ? ('0' + seconds) : seconds;
time = hour + ':'+ min + ':' + seconds;
_dom.innerHTML = time;
_dom.value = Timer.options.seconds;
Timer.options.seconds += 1;
},
startTimer: function(){
clearInterval(this.options.timer);
this.options.timer = setInterval(function(){
Timer.formatTime(Timer.getSeconds())
}, 1000);
},
pauseTimer: function(){
var _status = this.options.status
if (_status) {
clearInterval(this.options.timer);
this.options.timer = null;
this.options.status = 0;
}else{
this.startTimer();
this.options.status = 1;
}
},
stopTimer: function(){
clearInterval(this.options.timer);
this.options.timer = null;
Timer.options.seconds = 1;
}
}
window.Timer = window.Hourglass = Timer;
})(window);