Simple script to prompt user if they have not interacted with your page for a given amount of time. The check interval is 6 seconds and currently listens for the `mousemove` event but `scroll` or others can be used equally well.
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 PROMPT = { | |
prompt : false, | |
interval : 6000, | |
now : new Date(), | |
init : function() { | |
// Do initial check | |
this.listener(); | |
// console.log(this); | |
// Listen for mousemove, scroll etc | |
if (el.addEventListener) { | |
document.addEventListener('mousemove', this.listener, false); | |
} else if (el.attachEvent) { | |
document.attachEvent('mousemove', this.listener); | |
} | |
// Run check every n seconds | |
setInterval(this.checkForInteraction, this.interval); | |
}, | |
listener : function() { | |
PROMPT.now = new Date(); | |
PROMPT.checkForInteraction(); | |
}, | |
// Checks that are done every n seconds | |
checkForInteraction : function() { | |
var timeCheck = new Date(), | |
timeCheckString = timeCheck.getMinutes() + ':' + timeCheck.getSeconds(), | |
nowString = PROMPT.now.getMinutes() + ':' + PROMPT.now.getSeconds() | |
; | |
if ( timeCheckString !== nowString ) { | |
PROMPT.promptUser(); | |
} else { | |
// Run this function or do nothing | |
// PROMPT.dontPromptUser(); | |
} | |
}, | |
promptUser : function() { | |
this.prompt = true; | |
console.log('Do something!'); | |
}, | |
dontPromptUser : function() { | |
this.prompt = false; | |
console.log('Do nothing!'); | |
} | |
}; | |
// Start the prompt | |
PROMPT.init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment