Forum: Rails Spinoffs (closed, excessive spam) dynamically change frequency in periodicalUpdater

Posted by Debbie (Guest)
on 2008-06-13 19:51
(Received via mailing list)
Hello All,

I've seen this topic but it seems no one has post a response.  I'm
using periodicalUpdater to display live content queried from a
database.  Sometimes the result can be thousands of rows and before it
finishes processing (displaying as a visual indicator) all the data,
the next request is sent to the server. I'd like to have a
periodicalUpdater whose frequency can be set bases on the size of the
result set.  I've tried to set

onSuccess: function() {
this.frequency = dataSize * 5;
}

While this does change this.frequency, but when I tried to

alert(this.frequency) in subsequent onCreate callback, it remains the
value the PU is initialized with. (and the PU updates according to the
initial frequency).

Is there a way to change frequency in the exposed onSuccess or
onCreate call back?  Or how can I pass the dataSize to the
updateComplete in prototype.js, if that's the way to go?

Thanks for your time.
Debbie
Posted by Frederick Polgardy (Guest)
on 2008-06-13 21:05
(Received via mailing list)
Just changing the frequency property wouldn't cause the refresh rate to
change.  It's set up with a setInterval() call which is implemented in 
the
browser.  I'd either monkey patch PeriodicalUpdater to do a 
clearInterval()
followed by setInterval() with the new value, or just shut down the PU 
and
start up a new one.

-Fred

On Fri, Jun 13, 2008 at 12:51 PM, Debbie <liu9471@gmail.com> wrote:

>
> Hello All,
>
> I've seen this topic but it seems no one has post a response.  I'm
> using periodicalUpdater to display live content queried from a
> database.  Sometimes the result can be thousands of rows and before it
> finishes processing (displaying as a visual indicator) all the data,
> the next request is sent to the server. I'd like to have a
> periodicalUpdater whose frequency can be set bases on the size of the
> result set.  I've tried to set


--
Science answers questions; philosophy questions answers.
Posted by Debbie (Guest)
on 2008-06-25 19:29
(Received via mailing list)
Thanks Fred.

I've followed the second option and it worked.  Below is my code, in
case someone will find it helpful:

var updateCounter = 0;
function createUpdater(freq)
{
    updater = new Ajax.PeriodicalUpdater("ajaxDIV", "Tickets.jsp", {
        frequency: freq,
        evalScripts: true,
        onCreate: function()
        {
                // change update frequency when data size changes, and
after the initial load
            if (updateCounter != 0 && dataSize != this.frequency / 5)
            {
                alert("data size changed. frequency new: " + dataSize
* 5 + " old: " + this.frequency)
                updateCounter = 0;
                updater = createUpdater(dataSize * 5);
                this.updater.stop();
            }
        },
        onSuccess: function()
        {
            updateCounter ++;
        },
        parameters: Form.serialize($("form"))
    }
            )
            ;
    return updater;
}
Posted by Frederick Polgardy (Guest)
on 2008-06-25 19:58
(Received via mailing list)
Hmm, I'm not quite sure how this works. :)  You appear to be referring 
to
two different updaters (updater and this.updater), but they actually 
refer
to the same object because the function is unbound, and updater is a 
global.

And sadly, I was wrong the whole time, PeriodicalUpdater doesn't use a
setInterval(), but a delayed function call.  You can change the 
frequency
value at any time by calling updater.frequency = newValue.

Sorry for the wild goose chase, but you learned something right? ;-) 
(Like,
don't trust people who post to mailing lists?)

-Fred

On Wed, Jun 25, 2008 at 12:28 PM, Debbie <liu9471@gmail.com> wrote:

>        frequency: freq,
>                updater = createUpdater(dataSize * 5);
>            ;
>    return updater;
> }


--
Science answers questions; philosophy questions answers.
This topic is locked and can not be replied to.