localStorage如何设置过期时间?

至于localStorage如何设置过期时间?

可以对相应的键设置到期时间

expireTime = 60 * 1000; // 过期时间为60s 
存储
localStorage.setItem(key, value);
localStorage.setItem(key-expire, ((new Date()).valueOf() + expireTime);

获取
let key-expire = localStorage.getItem(key-expire)
let value = "";
// 当前时间 已经超过 设置的过期时间
if (key-expire < ((new Date()).valueOf()) {
    localStorage.removeItem(key);
} else {
    value = localStorage.getItem(key);
}
posted @ 2020-01-30 22:00  Draymonder  阅读(711)  评论(0编辑  收藏  举报