最近在一个项目,因为是物联网设备,防止用户长期打个这个页面,然后看到数据没有变化,所以在全局在这个判断功能,就是用户打开这个页面5分钟没有操作就自动刷新页面,重新获取数据
<script type="text/javascript">
//监察是否长时未操作
/**
* 超时校验
* @param timeOut
* 超时时间,单位:毫秒
*/
const checkTimeOut = (timeOut) => {
currentTime = new Date().getTime(); //更新当前时间
if (currentTime - oldTime >= timeOut) { //判断是否超时
location.reload()
}
}
//这个默认值必须为系统时间(因为可能登录之后就没有操作)
var oldTime = new Date().getTime()
var currentTime;
window.onload=function(){
window.setInterval('checkTimeOut(10*60*1000)',1000);
/**
* 鼠标悬浮时触发
*/
window.document.onmouseover = () => {
//重置时间
oldTime = new Date().getTime();
}
}
</script>