var Timer = function (a, b) { return this._init(a, b) }; Timer.prototype = { VERSION: 1, _init: function (a, b) { this._interval = 1000; this._timer = null; this._cbs = []; this._multipliers = []; this._tickCounts = []; this._canRun = []; this._stoppedThreads = 0; this._runOnce = false; this._startedAt = -1; this._pausedAt = -1; if (typeof (a) == "number") { this._interval = a } this.addCallback(b); return this }, _preset: function () { this._stoppedThreads = 0; this._startedAt = -1; this._pausedAt = -1; for (var a = 0; a < this._cbs.length; a++) { this._canRun[a] = true; this._tickCounts[a] = 0 } }, _ticks: function (b) { var c = this; for (var a = 0; a < this._cbs.length; a++) { if (typeof (this._cbs[a]) == "function" && this._canRun[a]) { this._tickCounts[a]++; if (this._tickCounts[a] == this._multipliers[a]) { this._tickCounts[a] = 0; if (this.runOnce()) { this._canRun[a] = false; this._stoppedThreads++ } window.setTimeout(c._cbs[a], 0) } } } if (this.runOnce() && this._stoppedThreads == this._cbs.length) { this.stop() } if (typeof (b) == "number") { this.stop().start(null, true) } }, runOnce: function (a) { if (typeof (a) == "undefined") { return this._runOnce } else { if (typeof (a) == "boolean") { this._runOnce = a } else { alert("Invalid argument for runOnce(...).\n\nUsage: runOnce(true | false) /*Default value: false*/\nor, runOnce() to get status") } } return this }, interval: function (a) { if (typeof (a) == "undefined") { return this._interval } else { if (typeof (a) == "number") { this._interval = Math.floor(a) } } return this }, stop: function (a) { if (this._timer) { if (!a) { this._pausedAt = -1 } try { window.clearInterval(this._timer) } catch (b) {} this._timer = null } return this }, isStopped: function () { return ((this._timer == null) && !this.isPaused()) }, start: function (c, a) { if (this.isPaused()) { return this.resume() } if (!this.isStopped()) { return this } if (!a) { this._preset() } var b = this._interval; if (typeof (c) == "number") { b = c } var d = this; this._timer = window.setInterval(function () { d._ticks(c) }, b); this._startedAt = (new Date()).getTime(); this._startedAt -= (this._interval - b); return this }, pause: function () { if (this._timer) { this._pausedAt = (new Date()).getTime(); this.stop(true) } return this }, isPaused: function () { return (this._pausedAt >= 0) }, resume: function () { if (this.isPaused()) { var a = this._interval - ((this._pausedAt - this._startedAt) % this._interval); this._pausedAt = -1; this.start(a, true) } return this }, restart: function () { return this.stop().start() }, addCallback: function (b, a) { if (typeof (b) == "function") { this._cbs.push(b); if (typeof (a) == "number") { a = Math.floor(a); this._multipliers.push((a < 1 ? 1 : a)) } else { this._multipliers.push(1) } this._tickCounts.push(0); this._canRun.push(true) } return this }, clearCallbacks: function () { this._cbs.length = 0; this._multipliers.length = 0; this._canRun.length = 0; this._tickCounts.length = 0; this._stoppedThreads = 0; return this } };