Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Constructor abstraction of setTimeout
- * @constructor
- * @param {function} handler Function to be called when the timer triggers
- * @param {Number} timeout Minimum delay which the timer will triggers
- * @param {Array} args Arguments to be passed to the handler function
- * @author Victor N. wwww.victorborges.com
- * @date 2017-03-24
- */
- function Timeout(handler, timeout, args) {
- var that = this;
- this.created = Date.now();
- this.lastTriggered = null;
- this.timeout = timeout;
- this.readyState = 1; //pending
- this.id = window.setTimeout(function() {
- that.lastTriggered = Date.now();
- that.readyState = 4; //done
- delete that.id;
- handler.apply(that, args);
- }, timeout);
- }
- Timeout.prototype.cancel = function() {
- if (this.readyState < 4) {
- this.readyState = 4;
- window.clearTimeout(this.id);
- delete this.id;
- return true;
- }
- return false;
- };
- /**
- * Constructor abstraction of setInterval
- * @constructor
- * @param {function} handler Function to be called when the timer triggers
- * @param {Number} timeout Minimum delay which the timer will triggers
- * @param {Array} args Arguments to be passed to the handler function
- * @author Victor N. wwww.victorborges.com
- * @date 2017-03-24
- */
- function Interval(handler, timeout, args) {
- var that = this;
- this.created = Date.now();
- this.previouslyTriggered = null;
- this.lastTriggered = null;
- this.n = 0;
- this.timeout = timeout;
- this.readyState = 1; //pending
- this.id = window.setInterval(function() {
- that.n++;
- that.previouslyTriggered = that.lastTriggered;
- that.lastTriggered = Date.now();
- that.readyState = 3; //running
- handler.apply(that, args);
- }, timeout);
- }
- Interval.prototype.cancel = function() {
- if (this.readyState < 4) {
- this.readyState = 4;
- window.clearInterval(this.id);
- delete this.id;
- return true;
- }
- return false;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement