JS in Vue judges to log out automatically without operating the interface for a long time (recommended)

Time:2020-2-2

Requirement Description: if the background does not request the interface for half an hour, it will return the ID to exit the login, but it needs to request the interface. Now, the front-end uses JS to judge that if the interface does not operate for half an hour, it will automatically jump to the login page.

Create a. JS file and introduce this JS (Vue framework) in main.js

Save current time when login is successfullocalStorage.setItem("lastTime",new Date().getTime());

Then update the time when you click

var lastTime = new Date().getTime();
var currentTime = new Date().getTime();
Var timeout = 30 * 60 * 1000; // set timeout: 30 points
 
window.onload = function () {
  window.document.onmousedown = function () {
    localStorage.setItem("lastTime",new Date().getTime());
  }
};
function checkTimeout() {
  Currenttime = new date(). Gettime(); // update the current time
  lastTime = localStorage.getItem("lastTime");
  // console.log(currentTime - lastTime);
  // console.log(timeOut);
  If (currenttime - lasttime > timeout) {// determine whether to timeout
    //Console.log ("timeout");
    var url = window.location.href;
    var newUrl=url.match(/(\S*)#/)[1];
    
    window.open(newUrl + '#/login','_self');
  }
}
 
/*Timer interval 30 seconds to check whether the page has not been operated for a long time*/
window.setInterval(checkTimeout, 30000);

Check every 30 seconds to see if 30 minutes have passed.

summary

The above is a brief introduction of JS in Vue. JS judges to log out automatically without operating the interface for a long time. I hope it will help you!

Recommended Today

Analysis of new data type instances of ES6 learning notes

An example of this paper describes the new data types of ES6 learning notes. To share with you for your reference, as follows: 1. Data deconstruction assignment 1. Deconstruction and assignment of arrays Basic usage: let [key1, key2…] = [value1, Value2…] Let [name, age, sex] = [‘xiaoming ‘, 24,’ male ‘]; console.log(name); console.log(age); console.log(sex); Note […]