首页 > 其他分享 >非正常关闭网页,在打开页面未登录直接进页面bug

非正常关闭网页,在打开页面未登录直接进页面bug

时间:2022-10-31 17:55:46浏览次数:64  
标签:网页 清除 token localStorage 时间 lastTime bug 页面

在浏览器触发onunload事件的时候使用localStorage保存一个当前时间,在浏览器触发onload读取保存的时间,再创建一个新的时间,如果时间间隔大于3s,则清除token。

代码放在App.vue 的mounted生命周期中

 window.onload = function () {
            let lastTime = localStorage.getItem("lastTime");
            const interval = 3 * 1000;
            // 如果没有上一次离开的时间或者时间间隔大于3s,就清除token
            if (!lastTime || new Date().getTime() - lastTime > interval) {
                localStorage.removeItem("token");
                console.log("清除token")
            }else{
                console.log("时间过短不清除token")
            }
        };
        window.onunload = function () {
            localStorage.setItem("lastTime", new Date().getTime());
        };

 

标签:网页,清除,token,localStorage,时间,lastTime,bug,页面
From: https://www.cnblogs.com/guohanting/p/16845198.html

相关文章