Skip to content

Instantly share code, notes, and snippets.

@mrmartineau mrmartineau/prompt.js
Last active Dec 20, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.