<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<button class="remenber">记住密码</button>
<script type="text/javascript">
//封装过期控制代码
function set(key1, value){
//获取时间戳
var curTime = new Date().getTime();
return localStorage.setItem(key1, JSON.stringify({
data: value,
time: curTime
}));
}
var state = 123;
set("bb", state); //存储时间戳,和存储数据;
function get(key1, exp) {
var data = localStorage.getItem(key1);
console.log(data)
//转为对象
var dataObj = JSON.parse(data);
console.log(dataObj)
if(new Date().getTime() - dataObj.time > exp) {
alert("信息已过期");
} else {
alert("信息没过期");
var dataObjDatatoJson = JSON.parse(dataObj.data)
return dataObjDatatoJson;
}
}
let remenber = document.querySelector(".remenber");
remenber.addEventListener("click", () => {
get("bb", 5000);
})
</script>
</body>
</html>
网友评论