-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdown-timer.js
More file actions
184 lines (158 loc) · 5.34 KB
/
countdown-timer.js
File metadata and controls
184 lines (158 loc) · 5.34 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
@author Alex King
@date 08/12/2016
*/
/**
* CountDownTimer is a timing object which can be used to call
* events for the given length of a time period. Events can also
* be assigned to trigger once the CountDownTimer has elapsed.
* @constructor
* @param {int} mills - Duration of the timer (in milliseconds)
* @param {requestCallback} tickCallback - Callback function to trigger on every tick interval
* @param {requestCallback} elapsedCallback - Callback function to trigger after timer has elapsed
*/
function CountDownTimer(mills, tickCallback, elapsedCallback){
//Validate parameters
_validateNonNegativeInt(mills, "mills");
_validateCallback(tickCallback, true, "tickCallback");
_validateCallback(elapsedCallback, true, "elapsedCallback");
//Private Fields
var _initialTime = mills; // intiatiate initial time with validated int
var _currentTime = mills; // intiatiate current time with validated int
var _tickDuration = 1000; // set default tick duration
this._tickInterval = null;
this._tickCallback = null;
this._elapsedCallback = null;
//Assign callbacks if they are passed as parameters
if(tickCallback){
this._tickCallback = tickCallback;
}
if(elapsedCallback){
this._elapsedCallback = elapsedCallback;
}
/*** Functions ***/
/** Start the CountDownTimer */
this.start = function(){
this.tick();
};
/**
* Stop the CountDownTimer. Duration remaining will be paused until
* start()ed again
*/
this.stop = function(){
if(this._tickInterval){
clearInterval(this._tickInterval); // clear interval, if exists
}
};
/**
* Tick the timer for every time interval until timer has elapsed
*/
this.tick = function(){
var thisRef = this; // store reference to 'this'
var currentTime = thisRef.getCurrentTime();
var tickDuration = _tickDuration;
// set next interval of tick function
thisRef._tickInterval = setInterval(function(){
var curTime = thisRef.getCurrentTime();
if( curTime > 0){ // if timer still has remaining time, update time
thisRef.setCurrentTime(Math.max((curTime - tickDuration), 0));
// if callback exists, trigger event
if(thisRef._tickCallback){
thisRef._tickCallback();
}
}else{ // if timer has elapsed, clear interval and trigger elapsed callback
clearInterval(thisRef._tickInterval);
// if callback exists, trigger event
if(thisRef._elapsedCallback){
thisRef._elapsedCallback();
}
}
}, this.getTickDuration()); // set interval using tickDuration
};
/**
* Reset the timer to its initial state.
*/
this.reset = function(){
_currentTime = _initialTime; // reset current time to initial time
this.stop(); // stop tick() events
this._tickInterval = null;
};
/**
* Get the initial time of the timer
* @return {int} _initialTime - Initial time set for the timer (in milliseconds)
*/
this.getInitialTime = function(){
return _initialTime;
};
/**
* Get the current time of the timer
* @return {int} _currentTime - Current time to set for the timer (in milliseconds)
*/
this.getCurrentTime = function(){
return _currentTime;
};
/**
* Set the current time of the timer
* @param {int} mills - Positive, non-zero time (in milliseconds) to set the current time
*/
this.setCurrentTime = function(mills){
_validateNonNegativeInt(mills, "mills");
_currentTime = mills; // set time
};
/**
* Get the tick duration of the timer
* @return {int} _tickDuration - Tick duration to set for the timer (in milliseconds)
*/
this.getTickDuration = function(){
return _tickDuration;
};
/**
* Set the tick duration of the timer
* @param {int} mills - Positive, non-zero time (in milliseconds) to set the tick duration
*/
this.setTickDuration = function(mills){
_validateNonNegativeInt(mills, "mills");
_tickDuration = mills; // set time
};
}
/*** Helper Functions ***/
/**
* Validate integers as positive and non-zero
* @param {int} num - Integer to validate
* @param {string} name - Name of the integer
* @throws Invalidated integer exception
* @return {int} num - Validated integer
*/
function _validateNonNegativeInt(num, name){
if(!name){
name = "variable";
}
if( typeof num !== 'number' ){
throw "ERROR: " + name + " must be defined as an integer.";
}
if( num < 0 ){
throw "ERROR: " + name + " must be a positive integer.";
}
return num;
};
/**
* Validate functions as callback functions
* @param {requestCallback} callback - Callback function
* @param {bool} isOptional - Whether or not the callback function is optional
* @param {string} name - Name of the callback function
* @throws Invalidated callback exception
* @return {requestCallback} callback - Validated callback function
*/
function _validateCallback(callback, isOptional, name){
if(isOptional){
if( (typeof callback !== 'undefined' || callback === null)
&& typeof callback !== 'function') {
throw "ERROR: " + name + " is optional, but must be a function, if defined.";
}
}else{
if( typeof callback !== 'function') {
throw "ERROR: " + name + " must be a function.";
}
}
return callback;
};