Skip to content

Instantly share code, notes, and snippets.

@park-brian
Last active August 8, 2018 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save park-brian/92c0fb9acf6dc3582b77f02728cf1a75 to your computer and use it in GitHub Desktop.
Save park-brian/92c0fb9acf6dc3582b77f02728cf1a75 to your computer and use it in GitHub Desktop.
onIdle.js
/**
* 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