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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
var AloTimer = (function (TIME_CHAIN, floor, Date, Error) {
// Note that this is not global if require()'d in NodeJS, only plain Javascript
/**
* Creates the timer
* @author Art <a.molcanovas@gmail.com>
* @param {number} [timeout=0] How many milliseconds to have the timeout for
* @param {string[]} [timeChain=["days", "hours", "minutes", "seconds"]] Which units to include in toString().
* Valid values are "days", "hours", "minutes", "seconds", and "ms". With the default setting the output for 1
* hour 53 minutes 11 seconds and 6 milliseconds would be 00:01:53:11.
* @class
* @global
*/
var AloTimer = function (timeout, timeChain) {
/**
* How long the timeout is set for
* @type {number}
*/
this.timeout = timeout || 0;
/**
* When the timeout started
* @type {number}
* @readonly
*/
this.timeStart = new Date().getTime();
/**
* Which units to include in toString()
* @type {string[]}
*/
this.timeChain = timeChain || TIME_CHAIN;
/**
* When the timer was paused. Will hold false if the timer isn't currently paused.
* @type {Date|boolean}
*/
this.pauseTime = false;
}, padLeft = function (str, char, length) {
while (str.length < length) {
str = char + str;
}
return str;
};
/** @global */
AloTimer.prototype = {
/**
* Adds milliseconds to the timer
* @author Art <a.molcanovas@gmail.com>
* @param val The amount to add
* @returns {AloTimer}
*/
addMS: function (val) {
this.timeout += val;
return this;
},
get timeout() {
return this._timeout;
},
set timeout(num) {
if (!isNaN(num)) {
this._timeout = parseInt(num);
} else {
throw new Error("The timeout must be numeric!");
}
},
/**
* Pauses the timer
* @author Art <a.molcanovas@gmail.com>
* @returns {AloTimer}
*/
pause: function () {
if (this.isPaused) {
console.warn("The timer is already paused");
} else {
this.pauseTime = new Date();
}
return this;
},
/**
* Checks if the timer is currently paused
* @author Art <a.molcanovas@gmail.com>
* @returns {boolean}
*/
get isPaused() {
return this.pauseTime instanceof Date;
},
/**
* Unpauses the timer
* @author Art <a.molcanovas@gmail.com>
* @returns {AloTimer}
*/
unpause: function () {
if (!this.isPaused) {
console.warn("The timer isn't paused");
} else {
this.addMS(this.msSincePause);
this.pauseTime = false;
}
return this;
},
/**
* Returns the number of milliseconds that have passed since the timer was paused
* @author Art <a.molcanovas@gmail.com>
* @returns {number}
*/
get msSincePause() {
if (this.isPaused) {
return new Date().getTime() - this.pauseTime;
} else {
return 0;
}
},
/**
* Adds seconds to the timer
* @author Art <a.molcanovas@gmail.com>
* @param val The amount to add
* @returns {AloTimer}
*/
addSeconds: function (val) {
this.timeout += val * 1000;
return this;
},
/**
* Adds minutes to the timer
* @author Art <a.molcanovas@gmail.com>
* @param val The amount to add
* @returns {AloTimer}
*/
addMinutes: function (val) {
this.timeout += val * 60000;
return this;
},
/**
* Adds hours to the timer
* @author Art <a.molcanovas@gmail.com>
* @param val The amount to add
* @returns {AloTimer}
*/
addHours: function (val) {
this.timeout += val * 3600000;
return this;
},
/**
* Adds days to the timer
* @author Art <a.molcanovas@gmail.com>
* @param val The amount to add
* @returns {AloTimer}
*/
addDays: function (val) {
this.timeout += val * 86400000;
return this;
},
/**
* Subtracts milliseconds from the timer
* @author Art <a.molcanovas@gmail.com>
* @param val The amount to add
* @returns {AloTimer}
*/
subMS: function (val) {
this.timeout -= val;
return this;
},
/**
* Subtracts seconds from the timer
* @author Art <a.molcanovas@gmail.com>
* @param val The amount to add
* @returns {AloTimer}
*/
subSeconds: function (val) {
this.timeout -= val * 1000;
return this;
},
/**
* Subtracts minutes from the timer
* @author Art <a.molcanovas@gmail.com>
* @param val The amount to add
* @returns {AloTimer}
*/
subMinutes: function (val) {
this.timeout -= val * 60000;
return this;
},
/**
* Subtracts hours from the timer
* @author Art <a.molcanovas@gmail.com>
* @param val The amount to add
* @returns {AloTimer}
*/
subHours: function (val) {
this.timeout -= val * 3600000;
return this;
},
/**
* Subtracts days from the timer
* @author Art <a.molcanovas@gmail.com>
* @param val The amount to add
* @returns {AloTimer}
*/
subDays: function (val) {
this.timeout -= val * 86400000;
return this;
},
/**
* Checks whether the timeout has finished
* @author Art <a.molcanovas@gmail.com>
* @returns {boolean}
*/
get hasFinished() {
return (new Date().getTime() - this.timeStart) >= this.timeout;
},
/**
* Returns the amount of milliseconds remaining
* @author Art <a.molcanovas@gmail.com>
* @returns {number}
*/
get msLeft() {
var diff = this.diff;
return diff < this.timeout ? (this.timeout - diff) % 1000 : 0;
},
/**
* Returns the difference between timeStart and now, taking pause time into account
* @author Art <a.molcanovas@gmail.com>
* @returns {number}
*/
get diff() {
return new Date().getTime() - this.timeStart - this.msSincePause;
},
/**
* Returns the amount of seconds remaining
* @author Art <a.molcanovas@gmail.com>
* @returns {number}
*/
get secondsLeft() {
var diff = this.diff;
return diff < this.timeout ? floor(((this.timeout - diff) / 1000) % 60) : 0;
},
/**
* Returns the amount of minutes remaining
* @author Art <a.molcanovas@gmail.com>
* @returns {number}
*/
get minutesLeft() {
var diff = this.diff;
return diff < this.timeout ? floor(((this.timeout - diff) / 60000) % 60) : 0;
},
/**
* Returns the amount of hours remaining
* @author Art <a.molcanovas@gmail.com>
* @returns {number}
*/
get hoursLeft() {
var diff = this.diff;
return diff < this.timeout ? floor(((this.timeout - diff) / 3600000) % 24) : 0;
},
/**
* Returns the amount of days remaining
* @author Art <a.molcanovas@gmail.com>
* @returns {number}
*/
get daysLeft() {
var diff = this.diff;
return diff < this.timeout ? floor((this.timeout - diff) / 86400000) : 0;
},
/**
* Returns a string representation of the amount of time remaining
* @author Art <a.molcanovas@gmail.com>
* @returns {string}
*/
toString: function () {
var arr = [], tmp, i;
for (i = 0; i < this.timeChain.length; i++) {
tmp = this[this.timeChain[i] + "Left"];
arr.push(padLeft(this[this.timeChain[i] + "Left"].toString(), "0", this.timeChain[i] === "ms" ? 3 : 2));
}
return arr.join(":");
}
};
return AloTimer;
})(['days', 'hours', 'minutes', 'seconds'], Math.floor, Date, Error);
if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
module.exports = AloTimer;
}