Skip to content

Instantly share code, notes, and snippets.

@cuth
Last active December 18, 2015 05:19
Show Gist options
  • Save cuth/5732072 to your computer and use it in GitHub Desktop.
Save cuth/5732072 to your computer and use it in GitHub Desktop.
Add this to any class to add timeout capabilities.
(function (exports, $) {
"use strict";
exports.Timer = function (opts) {
var defaults = {
delay: 5000,
start: null
};
this.opts = $.extend(defaults, opts);
this.timeout = null;
this.onHold = false;
return this;
};
exports.Timer.prototype = {
start: function (unhold) {
var self = this;
if (unhold) this.onHold = false;
if (this.onHold) return;
this.timeout = setTimeout(function () {
if (typeof self.opts.start === 'function') {
self.opts.start();
}
}, this.opts.delay);
},
stop: function () {
clearTimeout(this.timeout);
this.timeout = null;
},
restart: function () {
this.stop();
this.start();
},
hold: function () {
this.stop();
this.onHold = true;
}
};
}(this, jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment