Skip to content

Instantly share code, notes, and snippets.

@alkrauss48
Last active October 24, 2017 17:53
Show Gist options
  • Save alkrauss48/9b85c320da119e299ad12309c04a26e3 to your computer and use it in GitHub Desktop.
Save alkrauss48/9b85c320da119e299ad12309c04a26e3 to your computer and use it in GitHub Desktop.
Pure JS way to handle mouse- and keyboard-inactivity from a user
var inactivityTime = function () {
var t;
window.onload = resetTimer;
// DOM Events
document.onmousemove = resetTimer;
document.onkeypress = resetTimer;
function inactivityLogout() {
// optional - turn off the event handlers
// useful for single page apps
window.onload = null;
document.onmousemove = null;
document.onkeypress = null;
// Do something
}
function resetTimer() {
clearTimeout(t);
t = setTimeout(inactivityLogout, 15 * 60 * 1000) // 15 min
}
};
inactivityTime();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment