-
Notifications
You must be signed in to change notification settings - Fork 31
/
jquery.stopwatch.js
153 lines (133 loc) · 5.1 KB
/
jquery.stopwatch.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
(function( $ ){
function incrementer(ct, increment) {
return function() { ct+=increment; return ct; };
}
function pad2(number) {
return (number < 10 ? '0' : '') + number;
}
function defaultFormatMilliseconds(millis) {
var x, seconds, minutes, hours;
x = millis / 1000;
seconds = Math.floor(x % 60);
x /= 60;
minutes = Math.floor(x % 60);
x /= 60;
hours = Math.floor(x % 24);
// x /= 24;
// days = Math.floor(x);
return [pad2(hours), pad2(minutes), pad2(seconds)].join(':');
}
//NOTE: This is a the 'lazy func def' pattern described at http://michaux.ca/articles/lazy-function-definition-pattern
function formatMilliseconds(millis, data) {
// Use jintervals if available, else default formatter
var formatter;
if (typeof jintervals == 'function') {
formatter = function(millis, data){return jintervals(millis/1000, data.format);};
} else {
formatter = defaultFormatMilliseconds;
}
formatMilliseconds = function(millis, data) {
return formatter(millis, data);
};
return formatMilliseconds(millis, data);
}
var methods = {
init: function(options) {
var defaults = {
updateInterval: 1000,
startTime: 0,
format: '{HH}:{MM}:{SS}',
formatter: formatMilliseconds
};
// if (options) { $.extend(settings, options); }
return this.each(function() {
var $this = $(this),
data = $this.data('stopwatch');
// If the plugin hasn't been initialized yet
if (!data) {
// Setup the stopwatch data
var settings = $.extend({}, defaults, options);
data = settings;
data.active = false;
data.target = $this;
data.elapsed = settings.startTime;
// create counter
data.incrementer = incrementer(data.startTime, data.updateInterval);
data.tick_function = function() {
var millis = data.incrementer();
data.elapsed = millis;
data.target.trigger('tick.stopwatch', [millis]);
data.target.stopwatch('render');
};
$this.data('stopwatch', data);
}
});
},
start: function() {
return this.each(function() {
var $this = $(this),
data = $this.data('stopwatch');
// Mark as active
data.active = true;
data.timerID = setInterval(data.tick_function, data.updateInterval);
$this.data('stopwatch', data);
});
},
stop: function() {
return this.each(function() {
var $this = $(this),
data = $this.data('stopwatch');
clearInterval(data.timerID);
data.active = false;
$this.data('stopwatch', data);
});
},
destroy: function() {
return this.each(function(){
var $this = $(this),
data = $this.data('stopwatch');
$this.stopwatch('stop').unbind('.stopwatch').removeData('stopwatch');
});
},
render: function() {
var $this = $(this),
data = $this.data('stopwatch');
$this.html(data.formatter(data.elapsed, data));
},
getTime: function() {
var $this = $(this),
data = $this.data('stopwatch');
return data.elapsed;
},
toggle: function() {
return this.each(function() {
var $this = $(this);
var data = $this.data('stopwatch');
if (data.active) {
$this.stopwatch('stop');
} else {
$this.stopwatch('start');
}
});
},
reset: function() {
return this.each(function() {
var $this = $(this);
data = $this.data('stopwatch');
data.incrementer = incrementer(data.startTime, data.updateInterval);
data.elapsed = data.startTime;
$this.data('stopwatch', data);
});
}
};
// Define the function
$.fn.stopwatch = function( method ) {
if (methods[method]) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.stopwatch' );
}
};
})( jQuery );