Skip to content

Instantly share code, notes, and snippets.

@mrmartineau
Last active December 20, 2015 12:19
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 mrmartineau/6130428 to your computer and use it in GitHub Desktop.
Save mrmartineau/6130428 to your computer and use it in GitHub Desktop.
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.
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