Skip to content

Instantly share code, notes, and snippets.

@robink
Created June 6, 2011 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robink/1010508 to your computer and use it in GitHub Desktop.
Save robink/1010508 to your computer and use it in GitHub Desktop.
Javascript Deferred Interval
var delayedTimeout = function( callback, initialTime, maxTime ) {
this.currentTime = initialTime;
this.callback = callback;
this.maxTime = 0 || maxTime;
this.nextTick();
}
delayedTimeout.prototype = {
stop : function () {
this.stopNext = true;
},
nextTick : function() {
if ( this.stopNext )
return;
if ( ! this.previousTime )
this.previousTime = 0;
var newTime = this.previousTime + this.currentTime;
this.previousTime = this.currentTime;
this.currentTime = newTime;
if ( newTime > this.maxTime )
newTime = this.maxTime;
var that = this;
this.timeout = setTimeout(function() { that.onTimeout( that ) } , newTime);
},
onTimeout : function( ctx ) {
var that = this;
if ( ctx )
that = ctx;
that.callback();
that.nextTick();
}
}
@robink
Copy link
Author

robink commented Jun 6, 2011

Of course, this sample is intended to be run with nodejs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment