views:

309

answers:

3

I have a

periodically_call_remote

that I want to fire right away (as if the code was present without the setTimeout) as well as call periodically. Is there a nice way to do this use PCR, or do I have to roll my own?

I'm using jQuery/jRails if it helps.

A: 

This is not a perfect solution, but the remote function is invoked in the first millisecond.

def periodically_call_remote_starting_now(options = {} )
  page.call("starting_now_count = 0")
  periodically_call_remote ( options.merge(:frequency => 0.001, :condition => "++starting_now_count == 1"))
  periodically_call_remote (options)
end
KandadaBoggu
+1  A: 

There's always the onload solution.

In your view where you have define the body tag:

<body onload="<%= remote_function( ==same options used with periodically_call_remote==) %>" >

Which will call the remote function on load, as well as let the magic involved with periodically_call_remote call it at the desired interval. Seeing as how the clock starts with respect to the periodically_call_remote interval at roughly the same time as onload kicks in. You should get the functionality you want.

EmFi
Well, that will call it onclick, but I know what you mean :PI ended up using the remote_function solution.
Daniel Huckstep
oops. I kind of copied the onclick statement from some other code I had and forgot to change it to onload.
EmFi
A: 

Ok, I don't have enough reputation to comment on KandadaBoggu's post, so writing my comment as an answer.

I liked KandadaBoggu's response the best, but had to make some changes to get the helper method to work. The code I came up with is below. Feel free to vote me down if my changes are noobish (but better yet, comment and let everyone know why). My changes to KandadaBoggu's code are as follows:

def periodically_call_remote_starting_now(options = {})    
  result = ""
  result << javascript_tag("starting_now_count = 0;")
  result << periodically_call_remote(options.merge(:frequency => 0.001, :condition => "++starting_now_count == 1"))
  result << periodically_call_remote(options)
end

Otherwise I get the following error:

undefined local variable or method `page' for #<ActionView::Base:0x7fc37a6e2658>
zizee