Skip to content

Instantly share code, notes, and snippets.

@iRyusa
Forked from robink/delayedTimeout.js
Created June 23, 2011 13:11
Show Gist options
  • Save iRyusa/1042506 to your computer and use it in GitHub Desktop.
Save iRyusa/1042506 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 () {
clearTimeout(this.timeout);
},
nextTick : function() {
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.nextTick();
that.callback();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment