Skip to content

Commit 049f172

Browse files
committed
add play and reset methods
1 parent ab88610 commit 049f172

File tree

2 files changed

+168
-137
lines changed

2 files changed

+168
-137
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ Options can be set by attribute `data-<name>="value"` or in the options hash on
1818

1919
**`stop: string`** Defines the counter limit - _eg: 10:00 (ten minutes)_
2020

21+
## Methods
22+
23+
**`play`** Play counter
24+
25+
**`reset`** Reset counter
26+
27+
**`stop`** Stop counter
28+
2129
## Events
2230

2331
**`counterStop`** Raised when the counter reach limit

src/jquery.counter.js

Lines changed: 160 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -10,144 +10,141 @@
1010
else definition(context['$']);
1111
}(this, function ($) {
1212

13-
var methods = {
14-
init: function(options) {
15-
16-
options = options || {};
17-
18-
var checkStop = function(data) {
19-
var stop = 0;
20-
var current = 0;
21-
$.each(data.parts, function(i, part) {
22-
stop += (stop * part.limit) + part.stop;
23-
current += (current * part.limit) + part.value;
24-
});
25-
return data.down ? stop >= current : stop <= current;
26-
};
13+
var checkStop = function(data) {
14+
var stop = 0;
15+
var current = 0;
16+
$.each(data.parts, function(i, part) {
17+
stop += (stop * part.limit) + part.stop;
18+
current += (current * part.limit) + part.value;
19+
});
20+
return data.down ? stop >= current : stop <= current;
21+
};
2722

28-
var tick = function() {
29-
var e = $(this);
30-
var data = e.data('counter');
31-
var i = data.parts.length - 1;
32-
while(i >= 0 ) {
33-
var part = data.parts[i];
34-
part.value += data.down ? -1 : 1;
35-
if (data.down && part.value < 0) {
36-
part.value = part.limit;
37-
} else if (!data.down && part.value > part.limit) {
38-
part.value = 0;
39-
} else {
40-
break;
41-
}
42-
i--;
43-
}
44-
refresh(e, i);
45-
if (checkStop(data)) {
46-
clearInterval(data.intervalId);
47-
e.trigger("counterStop");
48-
}
49-
};
23+
var tick = function() {
24+
var e = $(this);
25+
var data = e.data('counter');
26+
var i = data.parts.length - 1;
27+
while(i >= 0) {
28+
var part = data.parts[i];
29+
part.value += data.down ? -1 : 1;
30+
if (data.down && part.value < 0) {
31+
part.value = part.limit;
32+
} else if (!data.down && part.value > part.limit) {
33+
part.value = 0;
34+
} else {
35+
break;
36+
}
37+
i--;
38+
}
39+
refresh(e, i);
40+
if (checkStop(data)) {
41+
clearInterval(data.intervalId);
42+
e.trigger("counterStop");
43+
}
44+
};
5045

51-
var refresh = function(e, to) {
52-
var data = e.data('counter');
53-
var i = data.parts.length - 1;
54-
while (i >= to) {
55-
var part = data.parts[i];
56-
var digits = part.value + '';
57-
while (digits.length < part.padding) {
58-
digits = '0' + digits;
59-
}
60-
$.each(split(digits, ''), function(j, digit) {
61-
animate(e, i, j, digit);
62-
});
63-
i--;
64-
}
65-
};
46+
var refresh = function(e, to) {
47+
var data = e.data('counter');
48+
var i = data.parts.length - 1;
49+
var animateIJ = function(j, digit) {
50+
animate(e, i, j, digit);
51+
};
52+
while (i >= to) {
53+
var part = data.parts[i];
54+
var digits = part.value + '';
55+
while (digits.length < part.padding) {
56+
digits = '0' + digits;
57+
}
58+
$.each(split(digits, ''), animateIJ);
59+
i--;
60+
}
61+
};
6662

67-
var animate = function(e, ipart, idigit, digit) {
68-
var edigit = $($(e.children('span.part').get(ipart)).find('span.digit').get(idigit));
69-
edigit.attr('class', 'digit digit' + digit + ' digit' + edigit.text() + digit).text(digit);
70-
};
63+
var animate = function(e, ipart, idigit, digit) {
64+
var edigit = $($(e.children('span.part').get(ipart)).find('span.digit').get(idigit));
65+
edigit.attr('class', 'digit digit' + digit + ' digit' + edigit.text() + digit).text(digit);
66+
};
7167

72-
//from http://blog.stevenlevithan.com/archives/cross-browser-split
73-
var split = function(undef) {
68+
//from http://blog.stevenlevithan.com/archives/cross-browser-split
69+
var split = function(undef) {
7470

75-
var nativeSplit = String.prototype.split,
76-
compliantExecNpcg = /()??/.exec("")[1] === undef, // NPCG: nonparticipating capturing group
77-
self;
71+
var nativeSplit = String.prototype.split,
72+
compliantExecNpcg = /()??/.exec("")[1] === undef, // NPCG: nonparticipating capturing group
73+
self;
7874

79-
self = function (str, separator, limit) {
80-
// If `separator` is not a regex, use `nativeSplit`
81-
if (Object.prototype.toString.call(separator) !== "[object RegExp]") {
82-
return nativeSplit.call(str, separator, limit);
83-
}
84-
var output = [],
85-
flags = (separator.ignoreCase ? "i" : "") +
86-
(separator.multiline ? "m" : "") +
87-
(separator.extended ? "x" : "") + // Proposed for ES6
88-
(separator.sticky ? "y" : ""), // Firefox 3+
89-
lastLastIndex = 0,
90-
// Make `global` and avoid `lastIndex` issues by working with a copy
91-
separator = new RegExp(separator.source, flags + "g"),
92-
separator2, match, lastIndex, lastLength;
93-
str += ""; // Type-convert
94-
if (!compliantExecNpcg) {
95-
// Doesn't need flags gy, but they don't hurt
96-
separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags);
97-
}
98-
/* Values for `limit`, per the spec:
99-
* If undefined: 4294967295 // Math.pow(2, 32) - 1
100-
* If 0, Infinity, or NaN: 0
101-
* If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
102-
* If negative number: 4294967296 - Math.floor(Math.abs(limit))
103-
* If other: Type-convert, then use the above rules
104-
*/
105-
limit = limit === undef ?
106-
-1 >>> 0 : // Math.pow(2, 32) - 1
107-
limit >>> 0; // ToUint32(limit)
108-
while (match = separator.exec(str)) {
109-
// `separator.lastIndex` is not reliable cross-browser
110-
lastIndex = match.index + match[0].length;
111-
if (lastIndex > lastLastIndex) {
112-
output.push(str.slice(lastLastIndex, match.index));
113-
// Fix browsers whose `exec` methods don't consistently return `undefined` for
114-
// nonparticipating capturing groups
115-
if (!compliantExecNpcg && match.length > 1) {
116-
match[0].replace(separator2, function () {
117-
for (var i = 1; i < arguments.length - 2; i++) {
118-
if (arguments[i] === undef) {
119-
match[i] = undef;
120-
}
121-
}
122-
});
123-
}
124-
if (match.length > 1 && match.index < str.length) {
125-
Array.prototype.push.apply(output, match.slice(1));
75+
self = function (str, separator, limit) {
76+
// If `separator` is not a regex, use `nativeSplit`
77+
if (Object.prototype.toString.call(separator) !== "[object RegExp]") {
78+
return nativeSplit.call(str, separator, limit);
79+
}
80+
var output = [],
81+
flags = (separator.ignoreCase ? "i" : "") +
82+
(separator.multiline ? "m" : "") +
83+
(separator.extended ? "x" : "") + // Proposed for ES6
84+
(separator.sticky ? "y" : ""), // Firefox 3+
85+
lastLastIndex = 0,
86+
// Make `global` and avoid `lastIndex` issues by working with a copy
87+
separator = new RegExp(separator.source, flags + "g"),
88+
separator2, match, lastIndex, lastLength;
89+
str += ""; // Type-convert
90+
if (!compliantExecNpcg) {
91+
// Doesn't need flags gy, but they don't hurt
92+
separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags);
93+
}
94+
/* Values for `limit`, per the spec:
95+
* If undefined: 4294967295 // Math.pow(2, 32) - 1
96+
* If 0, Infinity, or NaN: 0
97+
* If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
98+
* If negative number: 4294967296 - Math.floor(Math.abs(limit))
99+
* If other: Type-convert, then use the above rules
100+
*/
101+
limit = limit === undef ?
102+
-1 >>> 0 : // Math.pow(2, 32) - 1
103+
limit >>> 0; // ToUint32(limit)
104+
while (match = separator.exec(str)) {
105+
// `separator.lastIndex` is not reliable cross-browser
106+
lastIndex = match.index + match[0].length;
107+
if (lastIndex > lastLastIndex) {
108+
output.push(str.slice(lastLastIndex, match.index));
109+
// Fix browsers whose `exec` methods don't consistently return `undefined` for
110+
// nonparticipating capturing groups
111+
if (!compliantExecNpcg && match.length > 1) {
112+
match[0].replace(separator2, function () {
113+
for (var i = 1; i < arguments.length - 2; i++) {
114+
if (arguments[i] === undef) {
115+
match[i] = undef;
116+
}
126117
}
127-
lastLength = match[0].length;
128-
lastLastIndex = lastIndex;
129-
if (output.length >= limit) {
130-
break;
131-
}
132-
}
133-
if (separator.lastIndex === match.index) {
134-
separator.lastIndex++; // Avoid an infinite loop
135-
}
118+
});
136119
}
137-
if (lastLastIndex === str.length) {
138-
if (lastLength || !separator.test("")) {
139-
output.push("");
140-
}
141-
} else {
142-
output.push(str.slice(lastLastIndex));
120+
if (match.length > 1 && match.index < str.length) {
121+
Array.prototype.push.apply(output, match.slice(1));
122+
}
123+
lastLength = match[0].length;
124+
lastLastIndex = lastIndex;
125+
if (output.length >= limit) {
126+
break;
143127
}
144-
return output.length > limit ? output.slice(0, limit) : output;
145-
};
146-
147-
return self;
148-
149-
}();
128+
}
129+
if (separator.lastIndex === match.index) {
130+
separator.lastIndex++; // Avoid an infinite loop
131+
}
132+
}
133+
if (lastLastIndex === str.length) {
134+
if (lastLength || !separator.test("")) {
135+
output.push("");
136+
}
137+
} else {
138+
output.push(str.slice(lastLastIndex));
139+
}
140+
return output.length > limit ? output.slice(0, limit) : output;
141+
};
142+
return self;
143+
}();
150144

145+
var methods = {
146+
init: function(options) {
147+
options = options || {};
151148
return this.each(function() {
152149
var e = $(this);
153150
var data = e.data('counter') || {};
@@ -170,6 +167,7 @@
170167
part.limit = parseInt(value, 10);
171168
part.value = parseInt(initial[initial.length - format.length + index] || 0, 10);
172169
part.value = part.value > part.limit ? part.limit : part.value;
170+
part.reset = part.value;
173171
part.stop = parseInt(stop ? stop[stop.length - format.length + index] : (data.down ? 0 : part.limit), 10);
174172
part.stop = part.stop > part.limit ? part.limit : part.stop;
175173
part.stop = part.stop < 0 ? 0 : part.stop;
@@ -178,9 +176,9 @@
178176
while (digits.length < part.padding) {
179177
digits = '0' + digits;
180178
}
181-
for(var i=0; i < digits.length; i++) {
182-
epart.append($('<span>').addClass('digit digit' + digits.charAt(i)).text(digits.charAt(i)));
183-
}
179+
$.each(split(digits, ''), function(dindex, dvalue){
180+
epart.append($('<span>').addClass('digit digit' + dvalue).text(dvalue));
181+
});
184182
e.append(epart);
185183
data.parts.push(part);
186184
} else {
@@ -196,14 +194,39 @@
196194
return this;
197195
});
198196
},
197+
play: function() {
198+
return this.each(function() {
199+
var e = $(this);
200+
var data = e.data('counter');
201+
if (!data.intervalId) {
202+
data.intervalId = setInterval($.proxy(tick, this), data.interval);
203+
}
204+
});
205+
},
206+
reset: function() {
207+
return this.each(function() {
208+
var e = $(this);
209+
var data = e.data('counter');
210+
$.each(data.parts, function(pindex, pvalue){
211+
pvalue.value = pvalue.reset;
212+
});
213+
refresh($(this), 0);
214+
if (data.intervalId) {
215+
clearInterval(data.intervalId);
216+
data.intervalId = setInterval($.proxy(tick, this), data.interval);
217+
}
218+
});
219+
},
199220
stop: function() {
200-
var e = $(this);
201-
var data = e.data('counter');
202-
clearInterval(data.intervalId);
203-
e.trigger("counterStop");
204-
return this;
221+
return this.each(function() {
222+
var e = $(this);
223+
var data = e.data('counter');
224+
clearInterval(data.intervalId);
225+
data.intervalId = 0;
226+
e.trigger("counterStop");
227+
});
205228
}
206-
}
229+
};
207230

208231
$.fn.counter = function(method) {
209232
// Method calling logic

0 commit comments

Comments
 (0)