I wanted to be able to do polling using a function like this:

$.interval(seconds, callbackFunction);

The interval function will perform the callbackFunction every x
seconds, unless the previous call is still executing (inspired from
Prototype's PeriodicalExecuter)
I have written one that works, but as I have still a bit green with
javascript I know it can be improved.
Here is what I've got:

/*
 *  interval
 *  by Steven Wicklund <[EMAIL PROTECTED]>
 *  A setInterval wrapper for jQuery
 *  So this works for 1 timer interval, but the currentlyExecuting
singleton will likely cause issues
 *  if there is more than one interval per page...
 */
jQuery.extend( {
        interval:function(frequency, fn) {
                this.currentlyExecuting = false;
                setInterval(function() {$.onTimerEvent(fn);}, frequency * 1000);
        },
/*
 * onTimerEvent courtesy Prototype PeriodicalExecuter()
 * (c) 2005 Sam Stephenson <[EMAIL PROTECTED]>
 * (MIT license see:  http://prototype.conio.net/)
 */
        onTimerEvent: function(callback) {
            if (!this.currentlyExecuting) {
              try {
                this.currentlyExecuting = true;
                callback();
              } finally {
                this.currentlyExecuting = false;
              }
            }
        }
});

Reply via email to