def yo
if new_data
update :text => format(new_data)
else
update :text => periodically_call_remote(:update => ‘self’,
:action => 'yo' ... )
end
end
The intent is to update a div until new data appears, then display it
once
and stop updating the div.
I haven’t typed this in yet because I suspect it won’t work.
I suspect that the browser will evaluate the JavaScript representation
of
periodically_call_remote(), then spawn a window timer. The timer will
send
AJAX, and the reply will contain another JavaScript representation of
periodically_call_remote(). The new JavaScript will not turn off the
original timer. Then the browser will evaluate it and spawn a window
timer.
The effect will keep rolling along until the browser slows down with
lots of
simultaneous AJAX calls…
How to fix this? How to keep updating until data arrive, then stop?
And I followed the advice “state things positively”. So the variable is
‘keep_polling’, and the condition is one tick simpler:
:condition=>‘keep_polling’.
Now for my next magical trick, I shall add a number to stop_polling, to
make
it stop_polling_2, to put many of these naughty things all over my page!
(Going forward, I will replace every periodically_call_remote() with a
single one that intelligently updates with replace_html, only those divs
that have changed. But that is another story!)