In my work life, we have an application our customers can fill out to sign up for our travel service. Because the application is a multi-step process, we store some objects as session variables. In a default ASP.NET installation, session state will last for 20 minutes before timing out and releasing resources. So if a customer visits our web site and sits there for 20 or more minutes without clicking a link or button, their session state is lost and any data with it goes away.
Normally this isn’t a big deal if a user is browsing a non-form web page but when session state is critical to completing a function of the web site, such as an application or check-out process, you really need a way to keep the session alive as long as possible. This can be achieved by pinging a web page on your server with a simple JavaScript timer and an Ajax call. Doing so will tell the web server that the user is still out there, alive and kicking, so make sure you (the server) remember the session state. Lets talk about how we can keep a session alive. First off, create a ping page. This page doesn’t do anything, doesn’t render any HTML, its only purpose is to be pinged. The content of the page would look like this:
file:ping.html
notice there's nothing here because the page doesn't
do anything :-)
-->
Seriously though, you just need a blank HTML page to act as a ping page. Next we call on jQuery to register an anonymous function that is called when the page is loaded and also use its ajax post method (if you’re not using jQuery, you can use the window.onload event to register our ping function).
function keepSessionAlive() {
$.post("ping.html");
}
$(function() { window.setInterval("keepSessionAlive()", 60000); });
/*
alternatively you could do the following:
window.onload = function() {
window.setInterval("keepSessionAlive()", 60000);
};
the downside of using the above method is that it can
later be overwritten if anyone else utilizes the
window.onload event. Hence the use of jQuery.
*/
// ]]>
</script>
The above JavaScript registers an onload jQuery function to set a timeout to call our ping page. Every minute, the ping page will be posted to, which will tell the server that our client is still active on the web site and thus should keep the client’s session alive.
This is a really handy piece of code to have when you have any sort of check-out process but make sure you use it sparingly. Sessions that are active take up memory on the web server. The more active sessions you have, the more resources are taken up and it will eventually slow down your web server.

its the most useful post in this subject,its works fine , thanks for sharing
thanks for the post.
Unless I am missing something, I think you need to use setInterval instead of setTimeout to have the function called every minute
(or to call again the setTimeout in keepSessionAlive())
Good call, Samuel! Can’t believe I missed that. It definitely should call “setInterval” instead of “setTimeout” otherwise it will just run that single time and not keep calling “keepSessionAlive” again.
Tks. Very simple and useful
Many thanks!!!
Is the ping.html file even necessary? In my testing I didn’t even have to have the file present. It was the act of trying to ping it which kept the session alive.
That’s a good question, Jeremy. While technically you can ping the server with a random URL to keep the session alive, the point of having the “ping.html” file there is to have a lightweight file the server could use for a response. This file would require minimum processing from the web server and would likely be cached.
What I wouldn’t recommend doing is pinging a URL that doesn’t exist. Depending on your web server configuration, this could easily fill up your error logs with unnecessary 404 errors and/or send out a large amount of error emails to whoever is monitoring system health. Does that make sense?