I want to be able to have a progress bar periodically updated after a
button is clicked, then when the server-side task is complete - the
progress bar is no longer updated (to avoid hammering the server). I
have attempted to write a plug-in that extends the functionality of the
AJAX library loadIfModified() function. This plug-in allows the contents
of a <div> to be periodically updated after a defined interval. The
plug-in should also allow the update to be cancelled, however I have not
been able to successfully implement this. Any suggestions how to fix
this?


Here's my code:

var intervalTracker = new function() {};
$.fn.updateIfModified = function (options) {
        // default settings
        this.defaultSettings = {
                name: 'noName',
                url: '/',
                interval: 5000
        };
        
        // merge the options with the default settings
        var settings;
        if (options) {
                settings = $.extend(this.defaultSettings, options);
        } else {
                settings = this.defaultSettings;
        }
        
        // if the interval is negative, clear it
        if (settings.interval < 0) {
                if (intervalTracker['interval_' + settings.name]) {
                        clearInterval(intervalTracker['interval_' +
settings.name]);
                }
        
        // otherwise create a new interval and add to tracker
        } else {
                intervalTracker['interval_' + settings.name] =
setInterval(function() {
                        $(this).loadIfModified(settings.url);
                }, settings.interval);
        }
        
        return this;
}

Here's how I start the updating:
        $('status-container').updateIfModified({name: 'pollStatus', url:
'status.jsp', interval: 1000});

... and here's how I would like to cancel the updating:
        $('status-container').updateIfModified({interval: -1});

_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to