better deferred call
This commit is contained in:
parent
58ac58ee34
commit
5ab67aba78
1 changed files with 36 additions and 1 deletions
|
|
@ -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;
|
||||
};
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue