geeky · non php code

jQuery session timeout countdown with jQueryUI dialog

Very nice jQuery session timeout countdown example with the use of the jQuery idleTimer plugin.

Here’s my example with the jQueryUI dialog as the warning.

var idleTime = 2000; // number of miliseconds until the user is considered idle
var initialSessionTimeoutMessage = 'Your session will expire in <span id="sessionTimeoutCountdown"></span> seconds.<br /><br />Click on <b>OK</b> to continue your session.';
var sessionTimeoutCountdownId = 'sessionTimeoutCountdown';
var redirectAfter = 10; // number of seconds to wait before redirecting the user
var redirectTo = ''; // URL to relocate the user to once they have timed out
var keepAliveURL = 'keepAlive.php'; // URL to call to keep the session alive
var expiredMessage = 'Your session has expired.  You are being logged out for security reasons.'; // message to show user when the countdown reaches 0
var running = false; // var to check if the countdown is running
var timer; // reference to the setInterval timer so it can be stopped
$(document).ready(function() {
	// create the warning window and set autoOpen to false
	var sessionTimeoutWarningDialog = $("#sessionTimeoutWarning");
		title: 'Session Expiration Warning',
		autoOpen: false,	// set this to false so we can manually open it
		closeOnEscape: false,
		draggable: false,
		width: 460,
		minHeight: 50,
		modal: true,
		beforeclose: function() { // bind to beforeclose so if the user clicks on the "X" or escape to close the dialog, it will work too
			// stop the timer

			// stop countdown
			running = false;

			// ajax call to keep the server-side session alive
			  url: keepAliveURL,
			  async: false
		buttons: {
			OK: function() {
				// close dialog
		resizable: false,
		open: function() {
			// scrollbar fix for IE
		close: function() {
			// reset overflow
	}); // end of dialog

	// start the idle timer

	// bind to idleTimer's idle.idleTimer event
	$(document).bind("idle.idleTimer", function(){
		// if the user is idle and a countdown isn't already running
		if($.data(document,'idleTimer') === 'idle' &amp;&amp; !running){
			var counter = redirectAfter;
			running = true;

			// intialisze timer
			// open dialog

			// create a timer that runs every second
			timer = setInterval(function(){
				counter -= 1;

				// if the counter is 0, redirect the user
				if(counter === 0) {
					window.location = redirectTo;
				} else {
			}, 1000);


Here’s my example with the jQueryUI dialog as the warning.

21 thoughts on “jQuery session timeout countdown with jQueryUI dialog

  1. Thanks, nice work! Between your work and the guy that created the idleTimer you thought through some things that would have bitten me by writing this myself.

  2. Hi,

    What should be done if i have Iframe openened in page. Any action in iframe, doesn’t impact your idleTime. If i use this in iframe; it will open jquery winodw inside.

    How do we use where we have multiple iframes.

  3. Have you figured out why the warning dialog is called immediately after closing a javascript alert in Chrome? That little issue makes this plugin unusable which is a real bummer because it’s great otherwise.

  4. love this, but I have a few questions questions….

    1) what type of code in the keepalive.php file will reset the session? just setting sessions_start(); again ? and this will restore the session for another default of 1440(24 min.) as set in the .ini file?

    2) does this script actually check to see if the session has expired? it seems as though we are just giving it a time limit to expire in and not really calculating from the time the actual session began. am I right here?


  5. If you are on some other browser tab/instance it does not bring the focus back to message box. Is there a way we can acheive this?


  6. How can i achieve this functionality

    When session expired i don’t want to see the page after Login directly when i copy paste the URL in the browser. Means when i get an alert and returned back to the main page unless and until i logged in again i don’t want to access the second page how can i achieve this.

    1. There is no code in there. It’s just a url you call to keep your session alive. Whatever it is for your application. It’s only an example.

  7. I have two buttons on my page one is logoff and another is keep session alive it’s working fine for aspx page but it’s not working for popup window, with popup window if i click on keep session alive then after it’s logging off.Does anyboday has idea about this? I am using jquery fancy box as popup window.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s