better deferred call

This commit is contained in:
nightwing 2012-10-29 00:04:57 +04:00
commit 5ab67aba78

View file

@ -134,7 +134,7 @@ exports.getMatchOffsets = function(string, regExp) {
return matches;
};
/* deprecated */
exports.deferredCall = function(fcn) {
var timer = null;
@ -166,4 +166,39 @@ exports.deferredCall = function(fcn) {
return deferred;
};
exports.delayedCall = function(fcn, defaultTimeout) {
var timer = null;
var callback = function() {
timer = null;
fcn();
};
var _self = function(timeout) {
timer && clearTimeout(timer);
timer = setTimeout(callback, timeout || defaultTimeout);
};
_self.delay = delayed;
_self.schedule = function(timeout) {
if (timer == null)
timer = setTimeout(callback, timeout || 0);
};
_self.call = function() {
this.cancel();
fcn();
};
_self.cancel = function() {
timer && clearTimeout(timer);
timer = null;
};
_self.isPending = function() {
return timer;
};
return _self;
};
});