Last active
August 8, 2018 20:25
-
-
Save park-brian/92c0fb9acf6dc3582b77f02728cf1a75 to your computer and use it in GitHub Desktop.
onIdle.js
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
/** | |
* Calls a function after a user has been idle for a specified period of time | |
* @param callback {Function} - The function to call | |
* @param delay {number} - The delay (in milliseconds) | |
*/ | |
function onIdle(callback, delay) { | |
var timeoutId; | |
var events = ["click", "mousedown", "mouseup", "focus", "blur", "keydown", "change", "mouseup", "click", "dblclick", "mousemove", "mouseover", "mouseout", "mousewheel", "keydown", "keyup", "keypress", "textInput", "touchstart", "touchmove", "touchend", "touchcancel", "resize", "scroll", "zoom", "focus", "blur", "select", "change", "submit", "reset"]; | |
function initTimeout() { | |
clearTimeout(timeoutId); | |
timeoutId = setTimeout(function() { | |
callback(); | |
events.forEach(function(event) { | |
removeEventListener(event, initTimeout); | |
}); | |
}, delay); | |
} | |
events.forEach(function(event) { | |
addEventListener(event, initTimeout); | |
}); | |
initTimeout(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment