I read an article today and returned to the login page with no operation timeout. Don’t say anything. Go code
Methods a
From No operation timeout return to login page
GetTime () let currentTime = new Date().getTime() const timeOut = 30 * 60 * 1000 Let timeRunning = window.setInterval(checkTimeout, 30 * 1000) 30 s/time window. The onload = function () {window. The document. The onmouseover = function () {lastTime = new Date (). The getTime () / / Document.onclick = function () {lastTime = new Date().getTime()} Window. The document. The onkeydown = function () {lastTime = new Date (). The getTime () / / update the lastTime} window. The document. Onmousewheel = Function () {lastTime = new Date().getTime()}} function checkTimeout () {currentTime = new Date().gettime () // update the currentTime if (currenttime-lasttime > timeOut) {// determine whether the timeOut occurs. // clear the timeRunning timing clearInterval(timeRunning) // clear the cache Window. The localStorage. The clear () / / jump to the login page / * * / jump code}}Copy the code
Method 2
Use anti-jitter principle
var debounce = function(fun, delay) {
let time = null
return function() {
let args = arguments
clearTimeout(time)
time = setTimeout(() = > {
fun.apply(this, args)
}, delay)
}
}
function handle() {
// Returns the code for the login page
}
window.onload = function() {
const logOut = debounce(handle, 180000) // Set the timeout period
logOut()
document.body.addEventListener('click', logOut)
document.body.addEventListener('keydown', logOut)
document.body.addEventListener('mousemove', logOut)
document.body.addEventListener('mousewheel', logOut)
}
Copy the code