Jump to content

clientside timeouts


chadmichael

Recommended Posts

Hello,I'm working on a web application. We need to enforce some sort of timeout of a users session ( automatically log them out; invalidate the current page, etc. ). We use java ( servlets ) on the server side, and it is easy to timeout there session on the server. If the session times out on the server, the next request sent from the browser can easily be intercepted and routed back to a login page or some other page that indicates to the user that the timeout has occurred. But this only occurs when the user, after a certain period of time, submits another request. We would like to do something even if the user doesn't send another request. For instance, could we run a javascript function after a certain amount of time? We could use the function to display a dialog or something. I guess the important thing is the ability to run afunction after a certain period of time. ThanksChad

Link to comment
Share on other sites

First of all, add this to your body tag:onload=timeLog()Add this to your head section:

<script type="text/javascript">function changeLog(){ur=(url of login page here)window.location=ur}function timeLog(){x=(put time in milliseconds of time out here)var logTime=setTimeout(changeLog,x)}</script>

That should work. But be warned: the person can, at most cases, just press the forward button and log in again. Unless you have something complex running on your server. But in any case, try it out, and see if it works for you. :)

Link to comment
Share on other sites

What Chocolate570 posts will work fine, but the only problem there is that it doesn't reset the timer if you move your mouse or scroll down the page. If your site has a lot of content or you are worried about someone walking away and coming back yu can use this:

<script language="javascript" type="text/javascript">//////////////////////////////// session timeout redirect ////////////////////////////////var seconds=1200;function countDown() {	if(seconds<=0) {	document.location.href="xxxxxx.xxx";	}	seconds--;	window.setTimeout("countDown()",1000);	}function resetCounter() {	seconds=1200;}</script>

Replace xxxxxx.xxx with the page that you want people to be redirected to when you time expires. I have this set to goto my log out function and the time is the same length as my overall user session - so that they sinc up and no one gets a session timeout error.

<body onload="countDown()" onchange="resetCounter()" onmousemove="resetCounter()" onclick="resetCounter()" onScroll="resetCounter()">

Link to comment
Share on other sites

sweet - glad you all like it. I was pretty happy when I found all the elements to make that script - took a few google hours but it's proven quite worth the time since its been running since June of 2005 on a site that sees 21,000 uniques users a week.

Link to comment
Share on other sites

  • 11 months later...
sweet - glad you all like it. I was pretty happy when I found all the elements to make that script - took a few google hours but it's proven quite worth the time since its been running since June of 2005 on a site that sees 21,000 uniques users a week.
Can you use the countdown function to countdown days?? I would like to have a countdown of 30 days or 15 days at which point at the end of the countdown period the person wouldbe directed to a separate siteI have tried date comparison and many other things but with no luckI really need help on this one.Thanks in advanceM~
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...