判断用户首次登录的两种方式
前言
hello,友友们。最近因为项目需求,需要在用户首次登入的时候进行判断,并给出弹窗提示,在网上参考了好多大佬的方式,觉得这两种还不错,有同样需求的可以看看哟。
第一种,通过浏览器判断用户是否首次登录
if (window.name == "") {
console.log("首次被加载");
window.name = "isRefresh";
this.open();
} else if (window.name == "isRefresh") {
console.log("页面被刷新");
}
第二种通过缓存的时间来判断是否首次登录
let firstDate = localStorage.getItem('firstDate')
// 获取当前时间(年月日)
let now = new Date().toLocaleDateString()
// 转换成时间戳
let time = Date.parse(new Date(now))
if (localStorage.getItem('firstDate')) {
if (time > firstDate) {
localStorage.setItem('firstDate', JSON.stringify(time))
}
} else {
this.open();
localStorage.setItem('firstDate', JSON.stringify(time))
}
结语
总计到此为止啦,因为还不是技术牛牛,有什么问题可以评论和私信哟,欢迎补充!最后,如果能点赞激励博主更好啦!