Skip to content

Commit 4a8c75c

Browse files
committed
Allow multiple timers to work with custom classes.
Fix #25
1 parent a91f922 commit 4a8c75c

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

jquery.simple.timer.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,47 +30,49 @@
3030
}(function($, window, document, undefined) {
3131

3232
var timer;
33-
var _options = {};
3433

3534
var Timer = function(targetElement){
35+
this._options = {};
3636
this.targetElement = targetElement;
3737
return this;
3838
};
3939

4040
Timer.start = function(userOptions, targetElement){
4141
timer = new Timer(targetElement);
42-
mergeOptions(userOptions);
42+
mergeOptions(timer, userOptions);
4343
return timer.start(userOptions);
4444
};
4545

46-
// Writes to `_options` object so that other
46+
// Writes to `this._options` object so that other
4747
// functions can access it without having to
4848
// pass this object as argument multiple times.
49-
function mergeOptions(opts) {
49+
function mergeOptions(timer, opts) {
5050
opts = opts || {};
5151
var classNames = opts.classNames || {};
5252

53-
_options.classNameSeconds = classNames.seconds || 'jst-seconds'
54-
, _options.classNameMinutes = classNames.minutes || 'jst-minutes'
55-
, _options.classNameHours = classNames.hours || 'jst-hours'
56-
, _options.classNameClearDiv = classNames.clearDiv || 'jst-clearDiv'
57-
, _options.classNameTimeout = classNames.timeout || 'jst-timeout';
53+
timer._options.classNameSeconds = classNames.seconds || 'jst-seconds'
54+
, timer._options.classNameMinutes = classNames.minutes || 'jst-minutes'
55+
, timer._options.classNameHours = classNames.hours || 'jst-hours'
56+
, timer._options.classNameClearDiv = classNames.clearDiv || 'jst-clearDiv'
57+
, timer._options.classNameTimeout = classNames.timeout || 'jst-timeout';
5858
}
5959

6060
Timer.prototype.start = function(options) {
6161

62+
var that = this;
63+
6264
var createSubDivs = function(timerBoxElement){
6365
var seconds = document.createElement('div');
64-
seconds.className = _options.classNameSeconds;
66+
seconds.className = that._options.classNameSeconds;
6567

6668
var minutes = document.createElement('div');
67-
minutes.className = _options.classNameMinutes;
69+
minutes.className = that._options.classNameMinutes;
6870

6971
var hours = document.createElement('div');
70-
hours.className = _options.classNameHours;
72+
hours.className = that._options.classNameHours;
7173

7274
var clearDiv = document.createElement('div');
73-
clearDiv.className = _options.classNameClearDiv;
75+
clearDiv.className = that._options.classNameClearDiv;
7476

7577
return timerBoxElement.
7678
append(hours).
@@ -93,7 +95,7 @@
9395
});
9496

9597
timerBoxElement.on('complete', function(){
96-
timerBoxElement.addClass(_options.classNameTimeout);
98+
timerBoxElement.addClass(that._options.classNameTimeout);
9799
});
98100

99101
timerBoxElement.on('complete', function(){
@@ -241,9 +243,9 @@
241243
return false;
242244
}
243245

244-
element.find('.' + _options.classNameSeconds).text(finalValues.pop());
245-
element.find('.' + _options.classNameMinutes).text(finalValues.pop() + ':');
246-
element.find('.' + _options.classNameHours).text(finalValues.pop() + ':');
246+
element.find('.' + this._options.classNameSeconds).text(finalValues.pop());
247+
element.find('.' + this._options.classNameMinutes).text(finalValues.pop() + ':');
248+
element.find('.' + this._options.classNameHours).text(finalValues.pop() + ':');
247249
};
248250

249251

tests/tests.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,29 @@ test('Accepts options for element classNames', function(){
324324
ok(timerElement.hasClass('banana-timeout'), 'Found timeout class');
325325
});
326326

327+
asyncTest('Runs independent timers with custom options', function(){
328+
expect(1);
329+
330+
var timerElement = $('#timer1');
331+
timerElement.data('seconds-left', 10);
332+
timerElement.startTimer();
333+
setTimeout(function() {
334+
equal(timerElement.text(), '00:00:08', 'Cleared timer');
335+
start();
336+
}, 2500);
337+
338+
var timerElement2 = $('#timer2');
339+
timerElement2.data('seconds-left', 5);
340+
341+
timerElement2.startTimer({
342+
classNames: {
343+
hours: 'banana-hours',
344+
minutes: 'banana-minutes',
345+
seconds: 'banana-seconds',
346+
clearDiv: 'banana-clearDiv',
347+
timeout: 'banana-timeout'
348+
}
349+
});
350+
351+
});
352+

0 commit comments

Comments
 (0)