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.

Revisions

  1. AlexR1712 revised this gist Jun 2, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion activity.js
    Original file line number Diff line number Diff line change
    @@ -17,4 +17,4 @@ window.setInterval(function(){

    console.log('Last Activity Date time', lastActivityDatetime);
    console.log('Seconds spended on site', timeActiveOnSite);
    console.log('Seconds spended on site', timeActiveOnSite);
    console.log('Seconds out the site', timeActiveOnSite);
  2. AlexR1712 created this gist Jun 2, 2018.
    20 changes: 20 additions & 0 deletions activity.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    // 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 spended on site', timeActiveOnSite);