faqts : Computers : Programming : Languages : JavaScript : Event handling

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

41 of 44 people (93%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How can I set an inactivity timer?

Apr 7th, 2000 15:17
Martin Honnen,


The following contains code which checks for mousemove and keyup events 
as user activity. Note that for NN4 that might not suffice to capture 
all user activity (for instance when a select has focus key events 
don't fire in NN4) but relevant activity should be caught. IE4+ and NN6 
should do fine.

<HTML>
<HEAD>
<SCRIPT>
var tid, time, action;
var message1 = 'You are getting lazy. No activity for ';
var message2 = ' seconds.';
function setInactivityTimer (time, action, repeat) {
  window.time = time;
  window.action = action;
  window.repeat = repeat;
  if (tid) 
    clearTimeout(tid);
  if (document.layers)
    document.captureEvents(Event.MOUSEMOVE | Event.KEYUP);
  document.onmousemove = document.onkeyup =
    function (evt) {
      setInactivityTimer(window.time, window.action, window.repeat);
      return true;
    };
  if (repeat)
    action += '; setInactivityTimer(' 
              + time + ', "' + action + '", ' + repeat + ');';
  else 
    action += '; clearEvents();';
  tid = setTimeout(action, time);
}
function clearEvents() {
  if (document.layers)
    document.releaseEvents(Event.MOUSEMOVE | Event.KEYUP);
  document.onmousemove = document.onkeyup = null;
}
</SCRIPT>
</HEAD>
<BODY ONLOAD="setInactivityTimer (30000, 
              'alert(message1 + (time / 1000) + message2);', true);"
>

            
</BODY>
</HTML>