Skip to content

Instantly share code, notes, and snippets.

@bhauman
Created February 23, 2010 20:38
Show Gist options
  • Select an option

  • Save bhauman/312675 to your computer and use it in GitHub Desktop.

Select an option

Save bhauman/312675 to your computer and use it in GitHub Desktop.
// This is a port of the prototype.js PeriodcialExecuter
// This should be framework independent
// Valuable if you like to/have to program in jQuery
function PeriodicalExecuter(callback, frequency) {
this.callback = callback;
this.frequency = frequency;
this.currentlyExecuting = false;
this.registerCallback();
}
PeriodicalExecuter.prototype = {
registerCallback: function() {
var me = this;
this.timer = setInterval(function() { me.onTimerEvent() }, this.frequency * 1000);
},
execute: function() {
this.callback(this);
},
stop: function() {
if (!this.timer) return;
clearInterval(this.timer);
this.timer = null;
},
onTimerEvent: function() {
if (!this.currentlyExecuting) {
try {
this.currentlyExecuting = true;
this.execute();
} finally {
this.currentlyExecuting = false;
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment