localStorage过期控制

原创 448894534 随笔 JavaScript 156阅读 2017-11-17 17:06:33 举报

function set(key ,v){
var curTime = new Date().getTime();
localStorage.setItem(key,JSON.stringify({data:v,time:curTime}));
}
function get(key,exp){
var data = localStorage.getItem(key);
var dataObj = JSON.parse(data);
if(new Date().getTime() - dataObj.time > exp){
console.log("expires");
}
else{
console.log("data = "+dataObj.data);
}
}

评论 ( 0 )
最新评论
暂无评论

赶紧努力消灭 0 回复