Can I use jquery to initiate a full page request instead of an asynchronous request?
I’m implementing a countdown timer for a testing app. I want the timing to persist through page requests, so I’m tracking the time server-side rather than in the browser. I have a jquery script poll an action every second to check the time and render the time remaining to an html element. My code:
View:
<div id='timer'></div>
application.js:
setInterval(function(){
$('#timer').load('/exams/time');
}, 1000);
Controller:
def time
time = check_time
if time
render :text => time
else
stop_exam
end
end
def stop_exam
@exam = Exam.find(session[:exam])
@exam.stop
reset_session
redirect_to @exam
end
The countdown works fine. However, after the redirect, @exam writes to rather than the replacing the whole page. Presumably it is treating the redirect as an AJAX request rather than a full page request.
How can I modify stop_exam to replace the full page (including the javascript so it stops polling?)
You could return a script that when loaded into the div will redirect. I don’t know ruby syntax but return something like this: