Skip to content

Instantly share code, notes, and snippets.

@wudong
Last active October 19, 2021 08:57
Show Gist options
  • Save wudong/58511d1d27bf0bba3e4d02ff8613158e to your computer and use it in GitHub Desktop.
Save wudong/58511d1d27bf0bba3e4d02ff8613158e to your computer and use it in GitHub Desktop.
function activateActivityTracker() {
window.addEventListener("mousemove", userActivityThrottler);
window.addEventListener("scroll", userActivityThrottler);
window.addEventListener("keydown", userActivityThrottler);
window.addEventListener("resize", userActivityThrottler);
}
var userActivityThrottlerTimeout = null
function userActivityThrottler() {
if (!userActivityThrottlerTimeout) {
userActivityThrottlerTimeout = setTimeout(() => {
resetUserActivityTimeout();
clearTimeout(userActivityThrottlerTimeout);
userActivityThrottlerTimeout = null;
}, USER_ACTIVITY_THROTTLER_TIME);
}
}
let userActivityTimeout = null;
function resetUserActivityTimeout() {
clearTimeout(userActivityTimeout);
userActivityTimeout = setTimeout(() => {
inactiveUserAction();
}, INACTIVE_USER_TIME_THRESHOLD);
}
function inactiveUserAction() {
// logout logic
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment