Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Event Handling

How to check activity on your page.
Posted: 5 Nov 02

Here is a little script I did to detect if users ar sleeping on a page . May be it can help some.

<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).

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close