-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
116 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,125 @@ | ||
var countdown = (function () { | ||
maltaF('./../node_modules/@fedeghe/interval/dist/index.js'); | ||
var isFunction = function (f) { return typeof f === 'function'; }; | ||
function Countdown (fn, horizont) { | ||
this.fn = fn; | ||
this.horizont = horizont; | ||
this.baseHorizont = horizont; | ||
this.to = null; | ||
this.active = false; | ||
this.paused = false; | ||
this._onErr = null; | ||
this._onEnd = null; | ||
this._onTick = null; | ||
this.tick = null; | ||
this.ticker = null; | ||
this.startPause = 0; | ||
this.pauseSpan = 0; | ||
this.updates = 0; | ||
var interval = require('@fedeghe/interval'), | ||
countdown = (function () { | ||
var isFunction = function (f) { return typeof f === 'function'; }; | ||
function Countdown (fn, horizont) { | ||
this.fn = fn; | ||
this.horizont = horizont; | ||
this.baseHorizont = horizont; | ||
this.to = null; | ||
this.active = false; | ||
this.paused = false; | ||
this._onErr = null; | ||
this._onEnd = null; | ||
this._onTick = null; | ||
this.tick = null; | ||
this.ticker = null; | ||
this.startPause = 0; | ||
this.pauseSpan = 0; | ||
this.updates = 0; | ||
|
||
this._onUpdate = null; | ||
this._update = null; | ||
this._onPause = null; | ||
this._pause = null; | ||
this._onResume = null; | ||
this._resume = null; | ||
} | ||
|
||
Countdown.prototype.end = function () { | ||
this.active = false; | ||
this._onEnd && this._onEnd(); | ||
this.ticker && this.ticker.end(); | ||
clearTimeout(this.to); | ||
return this; | ||
}; | ||
|
||
Countdown.prototype.onEnd = function (f) { if (isFunction(f)) { this._onEnd = f; } return this; }; | ||
this._onUpdate = null; | ||
this._update = null; | ||
this._onPause = null; | ||
this._pause = null; | ||
this._onResume = null; | ||
this._resume = null; | ||
} | ||
|
||
Countdown.prototype.onErr = function (f) { if (isFunction(f)) { this._onErr = f; } return this; }; | ||
Countdown.prototype.end = function () { | ||
this.active = false; | ||
this._onEnd && this._onEnd(); | ||
this.ticker && this.ticker.end(); | ||
clearTimeout(this.to); | ||
return this; | ||
}; | ||
|
||
Countdown.prototype.onPause = function (f) { if (isFunction(f)) { this._onPause = f; } return this; }; | ||
Countdown.prototype.onEnd = function (f) { if (isFunction(f)) { this._onEnd = f; } return this; }; | ||
|
||
Countdown.prototype.onResume = function (f) { if (isFunction(f)) { this._onResume = f; } return this; }; | ||
Countdown.prototype.onErr = function (f) { if (isFunction(f)) { this._onErr = f; } return this; }; | ||
|
||
Countdown.prototype.onUpdate = function (f) { if (isFunction(f)) { this._onUpdate = f; } return this; }; | ||
Countdown.prototype.onPause = function (f) { if (isFunction(f)) { this._onPause = f; } return this; }; | ||
|
||
Countdown.prototype.pause = function () { | ||
this.paused = true; | ||
this._onPause && this._onPause(this); | ||
this.to && clearTimeout(this.to); | ||
this.startPause = +new Date(); | ||
this.ticker && this.ticker.pause(); | ||
return this; | ||
}; | ||
Countdown.prototype.onResume = function (f) { if (isFunction(f)) { this._onResume = f; } return this; }; | ||
|
||
Countdown.prototype.resume = function () { | ||
this.paused = false; | ||
this._onResume && this._onResume(this); | ||
// reset horizont removing the pause | ||
this.pauseSpan = +new Date() - this.startPause; | ||
this.horizont -= this.pauseSpan; | ||
this.run(false, true); | ||
this.ticker && this.ticker.resume(); | ||
return this; | ||
}; | ||
Countdown.prototype.getStatus = function () { | ||
var now = +new Date(), | ||
elapsed = now - this.startTime - this.pauseSpan, | ||
remaining = this.horizont - elapsed + this.updates, | ||
progress = 100 * elapsed / this.horizont; | ||
return { elapsed: elapsed, remaining: remaining, progress: progress }; | ||
}; | ||
Countdown.prototype.run = function (onStart, avoid) { | ||
var self = this; | ||
this.startTime = this.startTime || +new Date(); | ||
this.active = true; | ||
onStart && onStart(self); | ||
this.to = setTimeout(function () { | ||
try { | ||
self.fn(); | ||
self.end(); | ||
self.ticker && self.ticker.end(); | ||
} catch (e) { | ||
self._onErr && | ||
self._onErr(e, self); | ||
self.active = false; | ||
} | ||
}, this.horizont); | ||
!avoid && this.ticker && this.ticker.run(); | ||
return this; | ||
}; | ||
Countdown.prototype.onUpdate = function (f) { if (isFunction(f)) { this._onUpdate = f; } return this; }; | ||
|
||
Countdown.prototype.update = function (amount) { | ||
var am = parseInt(amount), | ||
now = +new Date(), | ||
elapsed = now - this.startTime - this.pauseSpan, | ||
remaining = this.baseHorizont - elapsed, | ||
newHorizont = am + remaining; | ||
this.updates = am; | ||
this.baseHorizont = elapsed + newHorizont; | ||
if (newHorizont && newHorizont > 0) { | ||
this._onUpdate && this._onUpdate(this); | ||
this.horizont = newHorizont; | ||
Countdown.prototype.pause = function () { | ||
this.paused = true; | ||
this._onPause && this._onPause(this); | ||
this.to && clearTimeout(this.to); | ||
this.run(); | ||
} | ||
return this; | ||
}; | ||
this.startPause = +new Date(); | ||
this.ticker && this.ticker.pause(); | ||
return this; | ||
}; | ||
|
||
Countdown.prototype.onTick = function (fn, tick) { | ||
var self = this; | ||
this.ticker = interval(function (cycle) { | ||
Countdown.prototype.resume = function () { | ||
this.paused = false; | ||
this._onResume && this._onResume(this); | ||
// reset horizont removing the pause | ||
this.pauseSpan = +new Date() - this.startPause; | ||
this.horizont -= this.pauseSpan; | ||
this.run(false, true); | ||
this.ticker && this.ticker.resume(); | ||
return this; | ||
}; | ||
Countdown.prototype.getStatus = function () { | ||
var now = +new Date(), | ||
elapsed = now - self.startTime - self.pauseSpan, | ||
remaining = self.baseHorizont - elapsed, | ||
progress = parseFloat((100 * elapsed / self.baseHorizont).toFixed(3), 10); | ||
fn({ cycle: cycle, elapsed: elapsed, remaining: remaining, progress: progress }); | ||
}, tick); | ||
return this; | ||
}; | ||
elapsed = now - this.startTime - this.pauseSpan, | ||
remaining = this.horizont - elapsed + this.updates, | ||
progress = 100 * elapsed / this.horizont; | ||
return { elapsed: elapsed, remaining: remaining, progress: progress }; | ||
}; | ||
Countdown.prototype.run = function (onStart, avoid) { | ||
var self = this; | ||
this.startTime = this.startTime || +new Date(); | ||
this.active = true; | ||
onStart && onStart(self); | ||
this.to = setTimeout(function () { | ||
try { | ||
self.fn(); | ||
self.end(); | ||
self.ticker && self.ticker.end(); | ||
} catch (e) { | ||
self._onErr && | ||
self._onErr(e, self); | ||
self.active = false; | ||
} | ||
}, this.horizont); | ||
!avoid && this.ticker && this.ticker.run(); | ||
return this; | ||
}; | ||
|
||
Countdown.prototype.update = function (amount) { | ||
var am = parseInt(amount), | ||
now = +new Date(), | ||
elapsed = now - this.startTime - this.pauseSpan, | ||
remaining = this.baseHorizont - elapsed, | ||
newHorizont = am + remaining; | ||
this.updates = am; | ||
this.baseHorizont = elapsed + newHorizont; | ||
if (newHorizont && newHorizont > 0) { | ||
this._onUpdate && this._onUpdate(this); | ||
this.horizont = newHorizont; | ||
this.to && clearTimeout(this.to); | ||
this.run(); | ||
} | ||
return this; | ||
}; | ||
|
||
Countdown.prototype.onTick = function (fn, tick) { | ||
var self = this; | ||
this.ticker = interval(function (cycle) { | ||
var now = +new Date(), | ||
elapsed = now - self.startTime - self.pauseSpan, | ||
remaining = self.baseHorizont - elapsed, | ||
progress = parseFloat((100 * elapsed / self.baseHorizont).toFixed(3), 10); | ||
fn({ cycle: cycle, elapsed: elapsed, remaining: remaining, progress: progress }); | ||
}, tick); | ||
return this; | ||
}; | ||
|
||
return function (fn, horizont) { | ||
return new Countdown(fn, horizont); | ||
}; | ||
})(); | ||
return function (fn, horizont) { | ||
return new Countdown(fn, horizont); | ||
}; | ||
})(); | ||
(typeof exports === 'object') && (module.exports = countdown); |