Last active
October 24, 2017 17:53
-
-
Save alkrauss48/9b85c320da119e299ad12309c04a26e3 to your computer and use it in GitHub Desktop.
Pure JS way to handle mouse- and keyboard-inactivity from a user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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