// 默认 一天过期
function storeTime(KeyName, startTime = 86400000) {
let old = getStorageSync(KeyName);
if (!old) {
// 第一次进入设置时间缓存 86400000 默认一天
let oldTime = parseInt(Date.parse(new Date())) + startTime;
setStorageSync(KeyName, oldTime);
console.log("firstTime 第一次", oldTime);
return "firstTime";
} else {
let NewTime = parseInt(Date.parse(new Date()));
if (NewTime > old) {
// 超过 重新设置过期时间
NewTime += startTime;
setStorageSync(KeyName, NewTime);
// true 说明过期
return true;
} else {
return false;
}
}
}
export default storeTime;
就是个存储本地时间 看有没有过期 写的看着都不好看😂
1
yamedie 2020-08-05 15:51:38 +08:00
let old 之前可以写一行 let now = Date.now();
然后代码中的 parseInt(Date.parse(new Date()))都可以替换成 now 了 // 默认 一天过期 function storeTime(KeyName, offset = 86400000) { let now = Date.now(); let old = getStorageSync(KeyName); if (!old) { // 第一次进入设置时间缓存 86400000 默认一天 setStorageSync(KeyName, now + offset); console.log("firstTime 第一次", now + offset); return "firstTime"; } else { if (now > old) { // 超过 重新设置过期时间 setStorageSync(KeyName, now + offset); // true 说明过期 return true; } else { return false; } } } export default storeTime; |
3
johnnyNg 2020-08-05 16:38:03 +08:00
```javascript
// 默认 一天过期 function storeTime(KeyName, startTime = 86400000) { const old = getStorageSync(KeyName); let newTime = parseInt(Date.parse(new Date())); let statu = false; if (!old) { newTime += startTime; statu = "firstTime"; } else if (newTime > old) { newTime += startTime; statu = true; } setStorageSync(KeyName, newTime); return statu; } export default storeTime; ``` |
4
test005 2020-08-05 16:46:34 +08:00
```js
// 默认 一天过期 function storeTime(KeyName, startTime = 86400000) { const old = getStorageSync(KeyName) const current = parseInt(Date.parse(new Date()), 10) if ( old && current < old ) return false setStorageSync(KeyName, current + startTime) return !!old ? true : 'firstTime' } ``` |
5
rioshikelong121 2020-08-05 16:48:25 +08:00
//returnValue: {isFirstTime, isTimeout}
function storeTime(KeyName, timeOutStamp = 86400000) { let oldTimeStamp = getStorageSync(KeyName); let nowTimeStamp = parseInt(Date.parse(new Date()), 10); if (!oldTimeStamp || (nowTimeStamp > oldTimeStamp)) { let nextTimeoutStamp = nowTimeStamp + timeOutStamp; setStorageSync(KeyName, nextTimeoutStamp); return {isFirstTime: !oldTimeStamp, isTimeout: nowTimeStamp > oldTimeStamp}; } return {isFirstTime: false, isTimeout: false}; } export default storeTime; |