Autoclose the window after inactivity

This tutorial teaches you how to use JavaScript to set an inactivity timer and close a browser window. Provides a warning message using a hidden DIV.
HTML • JavaScript

Activity includes keyboard activity and mouse movement. The value of timePeriod in the example script represents 5 seconds (5000 milliseconds). Change to suit. For example 5 minutes = 300000 (1000 ms * 60 seconds * 5 minutes). The variable "warnPeriod" sets the amount of time that the warning div will appear before the window closes if the timer isn't reset.

Step 1. Enter script below on Between Head Tags tab in Layout HTML. Note: use MasterBorder HTML if you wish to apply script to pages sharing a common Master Border

var autoCloseTimer;
var timeoutObject;
var timePeriod = 5000; // 5 seconds
var warnPeriod = 15000;  // 15 seconds

function promptForClose() { = 'block';
 autoCloseTimer = setTimeout("definitelyClose()",warnPeriod);

function autoClose() { = 'block'; //shows message on page
 autoCloseTimer = setTimeout("definitelyClose()",timePeriod); //starts countdown to closure

function cancelClose() {
 clearTimeout(autoCloseTimer); //stops auto-close timer = 'none'; //hides message

function resetTimeout() {
 clearTimeout(timeoutObject); //stops timer
 timeoutObject = setTimeout("promptForClose()",timePeriod); //restarts timer from 0

function definitelyClose() {
  top.opener = self;

Step 2. Enter following code on Inside Body Tag in Layout HTML (all on one line)

onkeydown="resetTimeout();" onmousedown='resetTimeout();'  onload="timeoutObject=setTimeout('promptForClose()',timePeriod);"

Step 3.

a) Place a text box on your page where you want warning to appear.

b) Press Ctrl+T and paste in the DIV code below. Feel free to modify the appearance.

Change text to match actual time period set in script.

The contents of the div only appear when the timer runs down.

Clicking on cancel closes the div and resets the timer.

<div id='autoCloseDiv' style="display:none">
    <center><p>Inactivity warning!<br>This window will autoclose in 15 seconds unless you hit 'Cancel.'</p>
    <input type='button' value='Close' onclick='definitelyClose();' />
    <input type='button' value='Cancel' onclick='cancelClose();' />

Written by

Posted by fbrefere001 on Thursday November 29, 2007