Lightweight User Idle Tracker In Vanilla JavaScript

Category: Javascript , Recommended | March 7, 2019
Authorwillianjusten
Last UpdateMarch 7, 2019
LicenseMIT
Tags
Views359 views
Lightweight User Idle Tracker In Vanilla JavaScript

Yet another vanilla JavaScript user idle tracker library that checks user interactions (click, keypress, keydown, load, mousemove, scroll, touchmove, touchstart) and trigger a function when a user is inactive for X amount of time.

How to use it:

Install and import the user-idle-tracker as an ES6 module.

# Yarn
$ yarn add user-idle-tracker
# NPM
$ npm install user-idle-tracker --save
import UserIdleTracker from "user-idle-tracker";

For browser:

<script src="/lib/user-idle-tracker.min.js"></script>

Specify the idle time in milliseconds.

const idleTime = 3000;

Define a callback function which will be fired when the user goes idle.

function stillThere() {
  alert("Heeeey! Are you still there?");
}

Start the user idle tracker.

const tracker = new UserIdleTracker(stillThere, idleTime);

You Might Be Interested In:


Leave a Reply