Tuesday, March 28, 2006

Prototype PeriodicalExecuter.stop()

James Gregory has a written a simple extension for the PeriodicalExecutor class of prototype.js to add a stop() method to it.

PeriodicalExecuter.prototype.registerCallback = function() {
this
.intervalID = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
}

PeriodicalExecuter.prototype.stop =
function() {
clearInterval(
this.intervalID);
}
All it does is take the current registerCallback function (which creates the actual timer) and stores its returned interval ID which we then use in the new stop function with the native clearInterval method.

Complete article is here: http://www.metalmadness.co.uk/Blog/archive/2006/01/09/801.aspx