Skip to content

Instantly share code, notes, and snippets.

@gucheen
Created March 9, 2015 11:12
Show Gist options
  • Save gucheen/808f25775551ee5589f7 to your computer and use it in GitHub Desktop.
Save gucheen/808f25775551ee5589f7 to your computer and use it in GitHub Desktop.
detect mouse idle
var timeoutID;
function setup() {
this.addEventListener("mousemove", resetTimer, false);
this.addEventListener("mousedown", resetTimer, false);
this.addEventListener("keypress", resetTimer, false);
this.addEventListener("DOMMouseScroll", resetTimer, false);
this.addEventListener("mousewheel", resetTimer, false);
this.addEventListener("touchmove", resetTimer, false);
this.addEventListener("MSPointerMove", resetTimer, false);
startTimer();
}
setup();
function startTimer() {
// wait 2 seconds before calling goInactive
timeoutID = window.setTimeout(goInactive, 2000);
}
function resetTimer(e) {
window.clearTimeout(timeoutID);
goActive();
}
function goInactive() {
// do something
}
function goActive() {
// do something
startTimer();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment