JS设置localStorage有效期
var date = new Date().getTime();
设置localStorage的值
foowwLocalStorage.set("test", "你好", date + 10000);
获取localStorage的值
var data = foowwLocalStorage.get("test");
const foowwLocalStorage = {
set: function (key, value, ttl_ms) {
var data = { value: value, expirse: new Date(ttl_ms).getTime() };
localStorage.setItem(key, JSON.stringify(data));
},
get: function (key) {
var data = JSON.parse(localStorage.getItem(key));
if (data !== null) {
debugger
if (data.expirse != null && data.expirse < new Date().getTime()) {
localStorage.removeItem(key);
} else {
return data.value;
}
}
return null;
}
}