Skip to content

Instantly share code, notes, and snippets.

@Macagare
Created March 5, 2013 10:23
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 Macagare/5089308 to your computer and use it in GitHub Desktop.
Save Macagare/5089308 to your computer and use it in GitHub Desktop.
JS: idle timer with callback
( function() {
var timeoutDuration = 30;
var mouseMoved = 0;
var keyPressed = 0;
var getDocument = function() {
if ( window ) {
return window.document;
}
return document;
}
var setDuration = function( duration ) {
this.timeoutDuration = duration;
};
var init = function() {
console.log("init");
getDocument().addEventListener( 'DOMContentLoaded', onDocumentLoaded );
};
var onDocumentLoaded = function() {
console.log("onDocumentLoaded");
getDocument().onmousemove = onMouseMoved;
getDocument().onkeydown = onKeyPressed;
};
var onMouseMoved = function( event ) {
console.log( "onMouseMoved" );
this.mouseMoved = new Date();
};
var onKeyPressed = function( event ) {
console.log( "onKeyPressed" );
this.keyPressed = new Date();
};
init();
} )();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment