Skip to content

Instantly share code, notes, and snippets.

@rkeVader
Last active March 12, 2019 12:38
Show Gist options
  • Save rkeVader/5794892138c5d6830cb0f1662badae84 to your computer and use it in GitHub Desktop.
Save rkeVader/5794892138c5d6830cb0f1662badae84 to your computer and use it in GitHub Desktop.
POC Inactivity Timeout written for colleague
function inactivityTimeout(
timeoutWarningMinutes,
timeoutLogoutMinutes,
contentID,
warningCallback,
kickedOffCallback) {
// the # of seconds before we warn the user of inactivity...
this.timeoutWarningSeconds = timeoutWarningMinutes * 60;
// the # of seconds we wait after the inactivity warning to log the user out...
this.timeoutLogoutSeconds = timeoutLogoutMinutes * 60;
// a variable to hold the timer
this.logoutTimer = null;
// flag so we know if we are in the initial wait or the log out wait
this.logoutWarned = false;
this.content = document.getElementById(contentID);
//alert(contentID);
//alert(this.content);
function warn_or_logout() {
// are we at the end of an warning wait, or logout wait period
if (!this.logoutWarned) {
// haven't been warned, so do that...
console.log("First Idle Threshhold met. Should Warn User.");
// start the logout timer period
this.resetLogoutTimer(this.timeoutLogOutSeconds);
if (warningCallback) {
warningCallback();
}
// set our warned flag so we know that we should log out should the timer elapse...
this.logoutWarned = true;
} else {
// the user was warned of the dire consequences... sorry, but YOU ARE OUTTA HERE, YOU IDLE TURD!!!
console.log("Second Idle Threshhold met. Should Boot User.");
// misc clean up
clearTimeout(this.logoutTimer);
this.logoutWarned = false;
document.removeEventListener("mousemove", resetLogoutTimer.bind(this));
document.removeEventListener("mousedown", resetLogoutTimer.bind(this));
document.removeEventListener("mouseup", resetLogoutTimer.bind(this));
document.removeEventListener("keypress", resetLogoutTimer.bind(this));
document.removeEventListener("click", resetLogoutTimer.bind(this));
document.removeEventListener("scroll", resetLogoutTimer.bind(this));
document.removeEventListener("touchstart", resetLogoutTimer.bind(this));
if (kickedOffCallback) {
kickedOffCallback();
//} else {
// window.location.replace("http://aepnow/");
}
}
};
function resetLogoutTimer(timeoutSeconds) {
//alert(1);
// clear the current timer...
clearTimeout(this.logoutTimer);
// reset warning to false (ie: we are in an initial waiting period again)
this.logoutWarned = false;
// if we were passed in a number, we use it, otherwise use the default 'aka' warning seconds
if (typeof(timeoutSeconds) != "number")
timeoutSeconds = this.timeoutWarningSeconds;
// remove the following line - only here so I can debug and ensure the correct events were running this code...
this.content.style.color = "black";
this.content.innerHTML = timestamp() + ": resetTimer called: timeoutSeconds=" + this.timeoutSeconds + "<br />";
// restart the timer for the appropriate timeout length in ms (1000ms / second)
this.logoutTimer = setTimeout(this.warn_or_logout.bind(this), timeoutSeconds * 1000);
};
// set the timer to start when the document loads...
//window.onload = this.resetLogoutTimer;
//window.addEventListener("load", this.resetLogoutTimer);
//this.resetLogoutTimer(null);
// hook into the events that we want to reset the timeout... ie: events showing user activity
document.addEventListener("mousemove", this.resetLogoutTimer.bind(this));
document.addEventListener("mousedown", this.resetLogoutTimer.bind(this));
document.addEventListener("mouseup", this.resetLogoutTimer.bind(this));
document.addEventListener("keypress", this.resetLogoutTimer.bind(this));
document.addEventListener("click", this.resetLogoutTimer.bind(this));
document.addEventListener("scroll", this.resetLogoutTimer.bind(this));
document.addEventListener("touchstart", this.resetLogoutTimer.bind(this));
}
/*
// the following is just here to provide the timestamp string I used in my test code... most likely you want to remove this
Number.prototype.padLeft = function (n,str){
return Array(n-String(this).length+1).join(str||'0')+this;
}
// the following is just here to provide the timestamp string I used in my test code... most likely you want to remove this
function timestamp() {
var now = new Date();
var hh = now.getHours().padLeft(2);
var mm = now.getMinutes().padLeft(2);
var ss = now.getSeconds().padLeft(2);
return hh + ":" + mm + ":" + ss;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment