I have a question I am have been googling all day with no examples I can use and need to come up with something.

I want to have a Javascript session timeout which does it's own internal countdown ie: 5 mins left before your session expires etc

I then would like to not have an "alert box" but rather an html layer indicating "x" minutes before the session expires, this page that pops up should also if there is no action in the 5 minutes reload it self to indicate the session has expired.

eg:
First Alert: "You have 5 minutes before your session expires"
Click here to continue your session.
or
Click here to logout

if no action in 5 mins then change the page to display
"Your session has expired"


I am really weak on Javascript but can plod through it if I have good examples.

Recommended Answers

All 5 Replies

You can definitely do the timeout thing with setTimeout( 300000 ); (number of milliseconds in 5 minutes). The interesting part would be keeping it running on a different page. It would restart whenever you navigated to a new page, which might make this much better suited to server-side programming :D

Well that would be easy here is my concept.

I begin the session in php and then also load the javascript with a php echo statement based on the parameter sent with the URL
(This will handle request to continue the session)

As for the self updating page all I need to do there is to have a timer for the number of minutes to logout on that page with the required Javascript eg: if 5 mins to log out when that popup is loaded the countdown begins, if the countdown matures it redirects to the log out page and closes itself (or something like that)

What I dont have is a example script to look at so I can monkey around with it and get what I need finding one on the net has proven to be exhausting as there are so many but so far removed from what I need.

If you know somewhere I can locate a script like that a link or anything so I can modify I would appreciate it.

Thanks.

I have a question I am have been googling all day with no examples I can use and need to come up with something.

I want to have a Javascript session timeout which does it's own internal countdown ie: 5 mins left before your session expires etc

I then would like to not have an "alert box" but rather an html layer indicating "x" minutes before the session expires, this page that pops up should also if there is no action in the 5 minutes reload it self to indicate the session has expired.

eg:
First Alert: "You have 5 minutes before your session expires"
Click here to continue your session.
or
Click here to logout

if no action in 5 mins then change the page to display
"Your session has expired"


I am really weak on Javascript but can plod through it if I have good examples.

Hi,
You'll be having problems with navigating away from the page if there's no user input for the confirmation box. Its modal!

I wrote this script while forgetting about this fact.
But here it is, -you could make use of that part too in case you decide to create your custom confirmation box, which will not halt the script command to navigate away..., but right now that part of a function will not get a chance to complete its task for as long as the modal dialog is on stand.

onload = initSession
/* Define variables here */
var	timeout = 5;  // minutes
	timeout = timeout * 60;
var	m, s;

var	sessionExpired = "expiredSession.html" //enter location 
var	remainingTime = 'remainingTime';//ID of the element to display time here!


function initSession(){
	confirmTime(0);
	var	confirmed = 
	window.confirm
		("You have 5 minutes before your session expires" +
		"\nClick 'OK' to continue 'Cancel' to logout"    );
	remainingTime=document.getElementById(remainingTime)	
	if(confirmed){
		sessionTime(0);
		}
	else{
		document.location = //choose one of options given:
		document.referrer||history.back()||sessionExpired;
		}//will work as is, also...
	}

function sessionTime(i){ i++;
	if(i >= timeout){
		clearTimeout(onSession);
		//do something, before go ??! or go strait to:
		document.location = sessionExpired;
		}
		else{
			if(remainingTime){
				var d = new Date();
				d.setMinutes(0,( timeout-i));
					m = d.getMinutes(); 
					s = d.getSeconds();
				remainingTime.innerHTML = m +' : '+ s;
		}
		var onSession = window.setTimeout( "sessionTime("+i+")", 1000 );
	}
}		

function confirmTime(i){ i++;
	if(i >= timeout){
		clearTimeout(confirmationWait);
		document.location = sessionExpired;
		}
		else{
			var confirmationWait = window.setTimeout( "confirmTime("+i+")", 1000 )
		}
	}

Well, this would be it, for now...
good luck.

I think I'll move this to "code snippets" - since it is an interesting one and working perfectly ... perhaps shorten it a little

Thanks a lot Troy.

I now have a new wrinkle.

I want to load a div on the timeout which contains an iframe
concept:

1) The timeout will execute a document write function which opens a div with the top z-index

2) This div will actually load an iframe with a page which has its own timeout which does a redirect to my logout page and closes itself if the continue session link is not clicked.

3) if the continue session link is click it activates a login function quietly in a hidden iframe on the page and closes itself.

Can anyone help me with a script to do this ??

I would simply have to get the function to write the div with the session expiry alert written to screen then have that function execute when the warning period for session expiry comes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.