Click to See Complete Forum and Search --> : Javascript detecting client side timeout


beecheyp
January 31st, 2005, 08:16 AM
Hi All,

I would really like my web page to be able to detect that a certain amount of time has passed (without input from the user) and once it has passed (say 15 mins) throw up an alert informing the user they are going to be logged out.

Does anyone know of an easy way to accomplish this (I am a novice at this too).

Many thanks in advance,

Paul.

Dr. Script
January 31st, 2005, 04:18 PM
So every time the user does something, the timer resets? You could run a timeout that would be cleared and restarted whenever the mouse moves over the body or is clicked or a key is pressed. Is that what you want?

beecheyp
February 1st, 2005, 03:11 AM
Indeed, that is exactly what I wanted and have just about managed to implement it as well.

Many thanks,

P.

Dr. Script
February 1st, 2005, 03:11 PM
Add this to your page:<script type="text/javascript">

var tm;
onload = function() {
tm = setTimeout(bye,900000);
}

body.onclick = resetTm;
body.onkeypress = resetTm;
body.onmousemove = resetTm;

function bye() {
// rmeove comment blocks if you want a confirm message
// if(confirm('Logout?')) {
window.location = '/main.html';
// }
}

function resetTm() {
clearTimeout(tm);
setTimeout(tm,900000);
}

</script>I didn't have time to test it ... maybe you could scale it down to like 1000 (1 second) and test if it works.