五分钟跳转回登录页面

Preface

最近业务中有一个小需求是, 用户五分钟没有操作, 跳转回登录页面。


代码实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 页面最外层的dom节点
let income = this.$refs.income;

let timer;
// 函数防抖
function startTimer(){
clearTimeout(timer);
timer = setTimeout(function(){
// todo: 清空cookie 和 token
window.location.href="/login.html";
}, 5 * 60 * 1000);
}

income.onmousemove = income.onmousedown = income.onclick = income.ontouchstart = income.ontouchmove = income.ontouchend = startTimer;

// 进入页面, 触发一下事件
income.onclick();
关注我的微信公众号[李一二],即时看更多的文章