Skip to content

Instantly share code, notes, and snippets.

@AlexR1712
Last active June 2, 2018 01:12
Show Gist options
  • Save AlexR1712/68da3c0a78d8839b87c34c4433f759d2 to your computer and use it in GitHub Desktop.
Save AlexR1712/68da3c0a78d8839b87c34c4433f759d2 to your computer and use it in GitHub Desktop.
Time Activity
// Script for track user activity in page and out the page
var timeActiveOnSite = 0; // seconds
var timeInactive = 0; // seconds
var lastActivityDatetime;
window.setInterval(function(){
if(document['visibilityState'] === 'visible'){
timeActiveOnSite++;
lastActivityDatetime = Date.now();
} else {
timeInactive++;
var timeDiff = Date.now() + (timeInactive * 1000)
if( (timeDiff - Date.now()) > 10*1000 ) { // 10 seconds inactity invokes alert
alert('User is inactive, clear session data!');
}
}
},1000);
console.log('Last Activity Date time', lastActivityDatetime);
console.log('Seconds spended on site', timeActiveOnSite);
console.log('Seconds out the site', timeActiveOnSite);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment