Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check activity on your page.

Event Handling

How to check activity on your page.

by  Targol  Posted    (Edited  )
Here is a little script I did to detect if users ar sleeping on a page ;-). May be it can help some.
Code:
<html>
<head>
<script language="javascript">

  var oInterval;
  var MAX_WITHOUT_ACTIVITY=10000; //number of milliseconds without activity before reload.
  
  function InitPage() {
    var oHid_h = document.getElementById("hid_lastActivity_h");
    var oHid_mn = document.getElementById("hid_lastActivity_mn");
    var oHid_s = document.getElementById("hid_lastActivity_s");
    var oDate=new Date();
    oHid_h.value = oDate.getHours();
    oHid_mn.value = oDate.getMinutes();
    oHid_s.value = oDate.getSeconds();
    oInterval = window.setInterval("VerifyActivity()", 10000); // check every 10 seconds    
  }

  function Activity() {
    var oHid_h = document.getElementById("hid_lastActivity_h");
    var oHid_mn = document.getElementById("hid_lastActivity_mn");
    var oHid_s = document.getElementById("hid_lastActivity_s");
    var oDate=new Date();
    oHid_h.value = oDate.getHours();
    oHid_mn.value = oDate.getMinutes();
    oHid_s.value = oDate.getSeconds();
  }
  
  function VerifyActivity() {
    var oHid_h = document.getElementById("hid_lastActivity_h");
    var oHid_mn = document.getElementById("hid_lastActivity_mn");
    var oHid_s = document.getElementById("hid_lastActivity_s");
    var oDate=new Date(); // current date and time
    var oLastActDate=new Date(oDate.getYear(), oDate.getMonth(), oDate.getDate(), oHid_h.value, oHid_mn.value, oHid_s.value); // last activity date and time.
    if ((oDate.valueOf() - oLastActDate.valueOf()) > MAX_WITHOUT_ACTIVITY) 
       Alert("Are you sleeping ? You didn't do anything during the past " + (MAX_WITHOUT_ACTIVITY /1000) + " seconds.");
  }
  
</script>
    </head>
    <body onload="InitPage();" onmousemove="Activity();" onkeydown="Activity();">
        <input id="hid_lastActivity_h" type="hidden" value=""/>
        <input id="hid_lastActivity_mn" type="hidden" value=""/>
        <input id="hid_lastActivity_s" type="hidden" value=""/>
    </body>
</html>

- Change the value of "MAX_WITHOUT_ACTIVITY" to change the time before "alert" or anything you want to do.
- I didn't take in account the day so this script won't work if user logs at 23:59:59. (11:59:59PM).
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top