Skip to content

Instantly share code, notes, and snippets.

@kalanaw
Last active August 30, 2018 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalanaw/a85e2836f6191b6ff8005c02292f73a7 to your computer and use it in GitHub Desktop.
Save kalanaw/a85e2836f6191b6ff8005c02292f73a7 to your computer and use it in GitHub Desktop.
Example of tracking mouse movements on a browser window to detect user activity
<!DOCTYPE html>
<html>
<body>
<h2>Example of User Activity</h2>
<p>This page detects user activity by mouse movements.</p>
<p id="demo"></p>
<script>
function trigger(){
var timestamp = new Date();
document.getElementById("demo").innerHTML = "User activity detected at " + timestamp;
}
function registerUserActivityListeners (){
this.addEventListener("mousemove", trigger, false);
this.addEventListener("mousedown", trigger, false);
this.addEventListener("keypress", trigger, false);
this.addEventListener("DOMMouseScroll", trigger, false);
this.addEventListener("mousewheel", trigger, false);
this.addEventListener("touchmove", trigger, false);
this.addEventListener("MSPointerMove", trigger, false);
}
registerUserActivityListeners();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment