I am doing something similiar (to your example anyway) in which I was
trying to use Ajax.PeriodicalUpdater as a polling mechanism of sorts.
As explained by Christophe, Ajax.PeriodicalUpdater isn't quite
designed to handle that sort of thing.

To get around this problem, I wrote my own simplistic wrapper to meet
my needs that I'm sure you could adapt for whatever situation you are
in. It borrows heavily from the concept of the PeriodicalUpdater, but
without actually using Ajax.Updater and performing various bits of js
logic according to the returns.

I've posted the relevant bits below:
Ajax.Poller = Class.create();
Ajax.Poller.prototype = Object.extend(new Ajax.Base(), {
        initialize: function(container,url,options) {
                this.setOptions(options);
                this.onComplete = this.options.onComplete;
                
                this.frequency = ( this.options.frequency || 1);
                this.decay     = ( this.options.decay > 1 ? this.options.decay 
: 2 );

                this.updater   = {};
        
                this.container = container;
                this.url       = url;

                this.start();
        },

        start: function() {
                this.options.onComplete = this.updateComplete.bind(this);
                this.onTimerEvent();
        },

        stop: function() {
                this.updater.options.onComplete = undefined;
                clearTimeout(this.timer);
                (this.onComplete || 
Prototype.emptyFunction).apply(this,arguments);
        },

        updateComplete: function(request,json) {
                // put some customized logic here (ie: 
this.onSuccess.apply(this,arguments); )
                if ( request.responseText == this.lastText ) {
                        // no change
                        if ( this.options.decay) {
                                this.decay = this.decay * this.options.decay;
                        }
                } else {
                        Element.update(this.container,request.responseText);
                        this.decay = this.options.decay || 1;
                }
                this.lastText = request.responseText;
                this.timer = setTimeout(
                        this.onTimerEvent.bind(this),
                        this.decay * this.frequency * 1000
                );
        },

        onTimerEvent: function() {
                this.updater = new Ajax.Request(this.url,this.options);
        }
});

Just place whatever logic you need in the updater. The only thing I
don't like about this solution is the code redundancy and I'm going to
look into fixing this more elegantly later, but for now this hack
works just fine.

Hopefully you'll get inspiration from it to solve your problem.

-E


On 10/10/06, Christophe Porteneuve <[EMAIL PROTECTED]> wrote:
>
> Hey there,
>
> [EMAIL PROTECTED] a écrit :
> > The following snippet of code does not work as expected in IE using
> > prototype 1.5.0_rc1
> >
> > What should happen is that the "in onSuccess" alert should pop up every
> > 1.5 seconds.
>
> No.  You're mistaken in your understanding.  Ajax.PeriodicalUpdater's
> interval and decay mechanism only governs the moments at which there is
> a check for *content update*.  You will only get notified every time the
> content *changes*.  So if you aim at a static-content file, only the
> first request will fire a notification.
>
> If you need periodical display w/o regard for change, just use
> window.setInterval and a regular Ajax.Updater call (and don't forget to
> clear the interval on page unload).
>
> 'HTH
>
> --
> Christophe Porteneuve a.k.a. TDD
> "[They] did not know it was impossible, so they did it." --Mark Twain
> Email: [EMAIL PROTECTED]
>
> >
>


-- 
Eric Ryan Harrison, Developer
model13 Designs

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs
-~----------~----~----~----~------~----~------~--~---

Reply via email to