I have the following code:
def show
@game = $site.new_game_of_type(params[:id])
@game.start
end
def update_game_time
render :partial => 'countdown', :layout => false
end
and the countdown partial:
<div id=countdown> <%= @game.running_time %></div>
In show.html.erb I have:
Time: <div id="countdown"> </div>
<%= periodically_call_remote(:frequency => 0.5,
:update => 'countdown', :url => {:action => :update_game_time}) %>
I want to create a game in the page that is showed by show, so I will have lot of ajax moving there but the user will not click anywhere. All the actions of the controller would be called from Javascript.
I got an error of @game being nil. It is nil even in the method update_game_time
how can I make the @game variable to remain valid while the user is in that page?
Making a request to the server every 0.5 seconds is not such a good idea.
My suggestion is to load the
running_timeonce and then use javascript to increase/decrease it.Maybe you could do something like:
in the view and then use some javascript on it, to update the timer:
This is just a basic example, but you get the point. Anyway it’s better than to make a request to the server every half of second, for a timer that can work very well on the client side too.