设置localStorage过期时长
1、设置缓存
var nowTime = new Date().getTime();
var data = {
value: val, expirse: nowTime + 30000 }//设置30秒过期
window.localStorage.setItem(key, JSON.stringify(data))
2、获取缓存
var data = JSON.parse(window.localStorage.getItem(key));
if (data !== null) {
if (data.expirse != null && data.expirse < new Date().getTime()) {
window.localStorage.removeItem(key)
} else {
return data.value
}
}
return null