# Cancel periodically\_call\_remote?

**URL:** <https://www.ruby-forum.com/t/cancel-periodically-call-remote/110380>\
**Category:** Rails\
**Created:** [July 25, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/cancel-periodically-call-remote/110380 "2007-07-25T22:25:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [July 25, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/cancel-periodically-call-remote/110380/1 "2007-07-25T22:25:05Z")

</div>

So I have a periodically\_call\_remote call happenign every 5 seconds.  
After about 10-20 seconds at most, everything that it needs to do is  
done, and it doesn’t need to fire anymore.

Now, I can take advantage of the :condition argument to put in some  
javascript variable such that it won’t actually make the call to the  
server anymore. But the javascript timer is still firing–every five  
seconds it’s deciding not to make the call to the server anymore.

If I leave the window open long enough, or have enough such windows  
open, this starts causing noticeable slowdown in my browser. (Perhaps  
some kidn of Firefox memory leak, perhaps not, who knows). I really want  
the thing to just stop firing–not to fire every five seconds and check  
to see if it should make the XML call to server, but just not fire at  
all anymore, it’s done.

Is there any good way to do this?

Thanks for any advice.

Jonathan

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [July 26, 2007, 12:46am UTC](https://www.ruby-forum.com/t/cancel-periodically-call-remote/110380/2 "2007-07-26T00:46:12Z")

</div>

On Jul 25, 4:25 pm, Jonathan R. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> I really want  
> the thing to just stop firing–not to fire every five seconds and check  
> to see if it should make the XML call to server, but just not fire at  
> all anymore, it’s done.
> 
> Is there any good way to do this?

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

~Rusty

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [July 26, 2007, 12:46am UTC](https://www.ruby-forum.com/t/cancel-periodically-call-remote/110380/3 "2007-07-26T00:46:22Z")

</div>

Hello,

Have you tried this:  
[http://tr.openmonkey.com/articles/2006/01/23/conditional-periodically\_call\_remote-in-rails](http://tr.openmonkey.com/articles/2006/01/23/conditional-periodically_call_remote-in-rails)

Dmitry

On Jul 25, 11:25 pm, Jonathan R. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [July 26, 2007, 12:46am UTC](https://www.ruby-forum.com/t/cancel-periodically-call-remote/110380/4 "2007-07-26T00:46:27Z")

</div>

Rusty B. 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!

–  
Phlip  
[http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
^ assert\_xpath  
[O'Reilly Media - Technology and Business Training](http://tinyurl.com/23tlu5) ← assert\_raise\_message

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [July 26, 2007, 3:40pm UTC](https://www.ruby-forum.com/t/cancel-periodically-call-remote/110380/5 "2007-07-26T15:40:11Z")

</div>

Rusty B. wrote:

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

That’s an interesting idea. Haven’t tried it yet, but it led me to  
another idea, after looking at the source code generated by  
periodically\_call\_remote, and tryign to understand what your this.stop()  
did.

My new idea: stop using periodically\_call\_remote at all. Instead,  
generate the Prototype javascript myself manually (based on the source  
code I see periodically\_call\_remote generating), to do exactly the same  
thing, but save the Prototype PeriodicalExecuter js object in a js  
variable:

some\_js\_var = new PeriodicialExecuter(…

Now I can just call some\_js\_var.stop() at a future point. I think this  
should work.

If the periodically\_call\_remote function were enhanced to take an  
argument for a name of a js variable to use in this assignment (not sure  
what you’d name this argument!), that would allow the  
periodically\_call\_remote to be used itself, and take care of this FAQ!

Jonathan

> 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

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [August 27, 2008, 10:43am UTC](https://www.ruby-forum.com/t/cancel-periodically-call-remote/110380/6 "2008-08-27T10:43:23Z")

</div>

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:

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

Micah Winkelspecht

> My new idea: stop using periodically\_call\_remote at all. Instead,  
> generate the Prototype javascript myself manually (based on the source  
> code I see periodically\_call\_remote generating), to do exactly the same  
> thing, but save the Prototype PeriodicalExecuter js object in a js  
> variable:
> 
> some\_js\_var = new PeriodicialExecuter(…
> 
> Now I can just call some\_js\_var.stop() at a future point. I think this  
> should work.
> 
> If the periodically\_call\_remote function were enhanced to take an  
> argument for a name of a js variable to use in this assignment (not sure  
> what you’d name this argument!), that would allow the  
> periodically\_call\_remote to be used itself, and take care of this FAQ!
> 
> Jonathan

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [July 26, 2007, 7:27am UTC](https://www.ruby-forum.com/t/cancel-periodically-call-remote/110380/7 "2007-07-26T07:27:55Z")

</div>

On Jul 25, 5:58 pm, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rusty B. 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.

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

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [March 1, 2010, 9:01pm UTC](https://www.ruby-forum.com/t/cancel-periodically-call-remote/110380/8 "2010-03-01T21:01:29Z")

</div>

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:

> 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:

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [January 16, 2009, 10:34pm UTC](https://www.ruby-forum.com/t/cancel-periodically-call-remote/110380/9 "2009-01-16T22:34:50Z")

</div>

Another thing you can do is to change the frequency to a very high  
number after your desired condition is met. Technically, this doesn’t  
cancel it, but it works.

On Aug 27 2008, 2:43 am, Micah Winkelspecht \<rails-mailing-

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [June 11, 2010, 6:03am UTC](https://www.ruby-forum.com/t/cancel-periodically-call-remote/110380/10 "2010-06-11T06:03:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [March 2, 2010, 5:03pm UTC](https://www.ruby-forum.com/t/cancel-periodically-call-remote/110380/11 "2010-03-02T17:03:58Z")

</div>

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:

var myInterval;

// Wait for DOM to load  
$(document).ready(function() {  
startUpdater();

// Setup start/stop buttons  
$("#stop\_refresh").click(stopUpdater);  
$("#start\_refresh").click(startUpdater);  
});

function startUpdater() {  
myInterval = window.setInterval(doUpdate, 2000);  
}

function stopUpdater() {  
window.clearInterval(myInterval);  
return false;  
}

## function doUpdate() { var url = “url/to/new/content”; $("#content\_div").load(url); }

Note: this is also an Unobtrusive JavaScript solution, which is another  
bonus.
