Keeping Session Alive with jQuery
A while ago I wrote a Coldfusion application that essentially was just a multi-page form. A scenario popped up where users might sit on a page for a long time to answer questions. Specifically more than 20 minutes. Unfortunately, I was unable to change the session timeout within the Application.cfc file due to other conflicts it may cause.
I was stumped on what to do until a colleague just happened to stumble across a blog posting by Brandon Purcell. To resolve my dilemma, jQuery is going to be used to keep the session open as long as the user keeps the page open.
The first step is to create an empty Coldfusion page. Why empty? Just to be sure nothing on the page causes it to hang up. Next, we setup jQuery to call this page every X amount of seconds (just make sure its under your session timeout). The only thing you will need is jQuery. Note that some people like to refresh variable into their scripting language variable but I left it out of this as I wanted to make it friendly to any programming language.
<script src=”/js/jquery-1.3.2.min.js” type=”text/javascript”></script>
<script type=”text/javascript”>
$(document).ready(function(){
setTimeout(“callserver()”,6000);
});function callserver() {
var remoteURL = ‘/emptypage.cfm’;
$.get(remoteURL, function(data) { setTimeout(“callserver()”,6000); });
}
</script>
Leave a comment