Detect If The User Is Idle In jQuery - Inactivity

Detect If The User Is Idle In jQuery - Inactivity
File Size: 6.42 KB
Views Total: 85
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

A tiny and mobile-friendly jQuery idle detection plugin that detects user's activities (keyboard, mouse, touch, or custom events) and fires an 'Inactivity' event when the user has been idle for a certain amount of time.

Default Events To Track:

  • mouseEvents: mousemove, mousedown, mousewheel, wheel, DOMMouseScroll, MSPointerDown, MSPointerMove
  • keyboardEvents: keypress, keydown, keyup
  • touchEvents: touchstart, touchmove, touchend

How to use it:

1. Place the minified version of the Inactivity plugin after loading the latest jQuery.

<script src="/path/to/cdn/jquery.slim.min.js"></script>
<script src="/path/to/src/jquery.inactivity.min.js"></script>

2. Initialize the plugin on the whole document.

$(document).inactivity( {
  // options here
});

3. Fire an event when a user is idle.

$(document).on("inactivity", function(){
  // do something when a user is idle
});

4. Fire an event when a user is active.

$(document).on("activity", function(){
  // do something when a user is active
});

5. Determine whether to detect mouse events. Default: true.

$(document).inactivity( {
  mouse: false
});

6. Determine whether to detect keyboard events. Default: false.

$(document).inactivity( {
  keyboard: true
});

7. Determine whether to detect keyboard events. Default: false.

$(document).inactivity( {
  touch: true
});

8. Determine whether to use custom events. Default: ''.

$(document).inactivity( {
  customEvents: "customEventName"
});

9. Determine whether to trigger only the first "activity" event. Default: false.

$(document).inactivity( {
  triggerAll: false
});

This awesome jQuery plugin is developed by readyforaliens. For more Advanced Usages, please check the demo page or visit the official website.