Last active
December 18, 2015 05:19
-
-
Save cuth/5732072 to your computer and use it in GitHub Desktop.
Add this to any class to add timeout capabilities.
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
(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