Use session control to log out automatically after a long time of inactivity

When working on a project, it is generally considered that there is no operation for a long time, and it can automatically log out. Below is the use of session to achieve this function

// Use H5 sessionStorage to save the public js of login information, do not use jquery
 // Currently only used for the supply platform 
var _EXPIRE_TIME= 30 * 60 * 1000 ; // If there is no operation for 30 minutes, log out 
var _interval_handler=- 1 ;
(function () {
    // First determine whether the login cgpt_memberInfo is the session information stored in the browser 
  var memberObjStr = sessionStorage.getItem( " cgpt_memberInfo " );
   if (memberObjStr) { // Logged
     in // Refresh the last usage time 
    sessionStorage.setItem( " cgpt_lastVisitTime " , new Date().getTime());
    _interval_handler =setInterval(checkExpired, 10 * 1000 ); // Check once every 10 seconds, if it times out 
  }
})();

function checkExpired() {
  console.log( " Check for expiration every 10 seconds " +window.location.href+ " :: " + new Date());
   var storeLastTime=sessionStorage.getItem( " cgpt_lastVisitTime " )?sessionStorage.getItem( " cgpt_lastVisitTime " ): - 1 ;
   if (storeLastTime==- 1 ) cleareInterval(_interval_handler);
   else {
     if (( new Date()).getTime()-storeLastTime>_EXPIRE_TIME) {   // Expired
       // Delete sessionStorage information 
      alert(" Because you haven't done anything for a long time, the system has automatically logged out for you " );
      sessionStorage.clear();
      // Change the logged in part in the page header to the one that needs to be logged in 
      document.location.reload(); // Refresh the current page 
       // Exit the loop 
      clearInterval(_interval_handler);
    }
  }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325573234&siteId=291194637