Skip to content

Instantly share code, notes, and snippets.

@AlexR1712 AlexR1712/activity.js
Last active Jun 2, 2018

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