Countdown Timer, Alert & Redirect

What I am trying to do is create a logout timer that will show an alert box, but if ‘OK’ isn’t clicked, then redirect to another page. I can get the timer working, but I can’t seem to run the redirect script after the alert box pops up. I can make it redirect automatically, but I wanted to prompt the user before redirecting. I have done this with VBScript, but I would like a JS solution if at all possible.

Here is what I have so far.


<script language="javascript" type="text/javascript">
var interval = "";
var timeout = "";

function startInterval()
{
    interval = window.setInterval(showAlert,10000);
}
function stopInterval()
{
    window.clearInterval (interval);
    interval="";
}
function showAlert()
{
	startLogout();
	alert("You have been idle for a while. Click OK or you will be logged out in 1 minute");
}
function startLogout()
{
	timeout = window.setTimeout(doLogout, 5000);
}
function doLogout()
{
	location.href='login.asp?msg=1';
}

startInterval();
</script>

The first argument to setTimeout/Interval should be either a code string or a callback (function), and a function name as a string won’t be called unless you include the function operator pair (‘showAlertb[/b]’).

Even so…is this an ‘inactivity timer’? Would need some more input to qualify…

http://www.javascripter.net/faq/confirm.htm

Yep, it is an inactivity timer. The function calls do work, btw. The reference states that you can use either a reference or a function call. So both ways are correct.

http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/window.html#1203669

The question still looms: How do I get the script to run a timer after the alert box pops up?

No such thing as ‘the reference’…msdn? devedge? ECMA-262? Newer browsers recognize function pointers, others don’t; invoking the function explicitly is still the best bet. :slight_smile:

Still don’t see where the ‘inactivity’ comes in. Anyway: pop up a confirm box (see link above) and:


function showConfirm()
{
	startLogout();
	if (confirm("You have been idle for a while. Click OK or you will be logged out in 1 minute"))
		clearTimeout(timeout);
}
function startLogout()
{
	timeout = window.setTimeout('doLogout()', 60000);
}

I’m using this on our intranet, so I’m not going for cross-browser compatability.

I tried the Confirm Box before and I still had the same problem. It seems like it doesn’t handle the startLogout function once the alert box pops up.

I’m not really going for movement inactivity, but more for a simple safety mechanism to log the user out if the page hasn’t been refreshed or loaded in the given time.

I might just forego the alert box and automatically log them out. It’s not a big deal, but it would be nice to get a warning before it logs you out.

Could get annoying…but, you’re the doctor.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>untitled</title>
<script type="text/javascript">

var logoutID = null;

function startMonitor()
{
	setTimeout('showConfirm()', 10000);
}

function startLogout()
{
	logoutID = setTimeout('doLogout()', 5000);
}

function showConfirm()
{
	if (confirm("You have been idle for a while. Click OK or you will be logged out in 1 minute"))
	{
		clearTimeout(logoutID);
		startMonitor();
	}
	else startLogout();
}

function doLogout()
{
	alert('adios');
	//self.location = 'login.asp?msg=1';
}

startMonitor();

</script>
</head>
<body>
</body>
</html>

Maybe you can use that…

The problem seems to be…some browsers (IE) won’t execute script statements while a modal window is open, so you have to wait for the user to press the ‘cancel’ button to start the countdown. Ugh. Here’s a workaround, using a regular pop-up. Won’t help much if they’re blocked, of course.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>untitled</title>
<script type="text/javascript">

var warnwin = null;
var logoutID = null;

function popdoc()
{
	var sHTML =
	'<head><title>! USER PROMPT</title><script>onunload=function(){if(opener&&!opener.closed)opener.' +
	'stopredir();}<\\/script></head><body style="background:black;font:12px monospace;color:white;mar' +
	'gin:0;padding:12px;" onblur="setTimeout(\\'self.focus()\\',100)"><br />You have been idle for a w' +
	'hile.<br/><br/>Click <input type="button" value=" OK " style="font:11px monospace;" onclick="se' +
	'lf.close()" /> or you will be logged out in <span id="s">60</span> seconds.<br/><script>x=60;se' +
	'tInterval("y=(x<=10?\\'0\\':\\'\\');if(x>0)document.getElementById(\\'s\\').innerHTML=y+--x",1000)<\\/' +
	'script><br/></body>';
	return sHTML;
}

function logout_alert()
{
	if (warnwin && !warnwin.closed)
		warnwin.close();
	var w = 400;
	var h = 110;
	var l = (screen.availWidth - w) * .5;
	var t = (screen.availHeight - h) * .5;
	warnwin = open('javascript&#58;opener.popdoc()', 'warnwin', 'left='+l+',top='+t+',width='+w+',height='+h+',status=0');
	if (warnwin && !warnwin.closed)
		warnwin.focus();
	logoutID = setTimeout('doredir()', 61000);
}

function doredir()
{
	if (warnwin && !warnwin.closed)
		warnwin.close();
	self.location = 'http://www.sitepoint.com';
}

function stopredir()
{
	clearTimeout(logoutID);
	setTimeout('logout_alert()', 2000);
}

onload = stopredir;

</script>
</head>
<body style="background&#58;url(http://www.light-speed-web-graphics.com/stripes_horiz_darker/fastest_00100.GIF);">
</body>
</html>

Just did it for the practice, no harm done… :agree:

We are using IE on our intranet (where this app is running), so that must be why the script isn’t working correctly. I never thought about using a pop up window (brilliant!). I will give it a try ans see how it works. Thanks for the time you put into this. :slight_smile: