cancel periodically_call_remote?

You could try putting the periodically call remote inside a div, and
have the last periodic call clear the div.

~Rusty

Rusty Burchfield wrote:

You could try putting the periodically call remote inside a div, and
have the last periodic call clear the div.

This is a FAQ, and that's wrong because the setTimeout still exists,
and it will still reactivate that object in memory. The DIV only
creates it and pushes it into memory.

I say leave the dumb thing spinning on the user's browser. They have
CPU cycles - you don't!

Emphasis on try.

This might work for you. Create a variable and function, for example:
IWantToContinue = true;
function continueOrNot() {
if (IWantToContinue) return true;
this.stop();
return false;
}

Then set :condition=>"continueOrNot()", and set IWantToContinue to
false with rjs when you are done.

To test it you can set :condition=>"continueOrNot() || alert('still
executing...')"

~Rusty

I took this idea and implemented it in my app.

In ApplicationHelper, I overrode the periodically_call_remote function
with:

  def periodically_call_remote(options = {})
    variable = options[:variable] ||= 'poller'
    frequency = options[:frequency] ||= 10
    code = "#{variable} = new PeriodicalExecuter(function()
{#{remote_function(options)}}, #{frequency})"
    javascript_tag(code)
  end

This will store the PeriodicalExecuter in a variable. In order to set
the name of the variable, you can pass in a :variable option. By
default, it will use 'poller'.

So you can do:

periodically_call_remote(:url => some_url, :update => 'some_div',
:variable => 'my_var')

And when you want to stop the polling, you can inject the following line
of javascript into your page:

<script>
  my_var.stop();
</script>

Hope this helps anyone else who's run into this.

Micah Winkelspecht

This seems to be what I was loooking for.
Now what I would like to know is how can I restart the call?
Can I push it from the server based on new data now available?

What I am trying to accomplish is to only update a dom when data in the
database has changed.

Thanks

Micah Winkelspecht wrote:

Jason Dely wrote:

This seems to be what I was loooking for.
Now what I would like to know is how can I restart the call?
Can I push it from the server based on new data now available?

I ran into these issue myself. After much struggle and frustration, it
dawned on me that doing periodic updates with straight up JavaScript is
really pretty easy. And, I found it to be more flexible anyway.

Here is an example of my solution. This happens to be jQuery, but the
same applies to Prototype:

Micah Winkelspecht wrote:

  def periodically_call_remote(options = {})
    variable = options[:variable] ||= 'poller'
    frequency = options[:frequency] ||= 10
    code = "#{variable} = new PeriodicalExecuter(function()
{#{remote_function(options)}}, #{frequency})"
    javascript_tag(code)
  end

Thanks alot! Helped me fix the problem as well.