[js] 模拟 localStorage 时如何实现过期时间功能
生活随笔
收集整理的這篇文章主要介紹了
[js] 模拟 localStorage 时如何实现过期时间功能
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
[js] 模擬 localStorage 時如何實現(xiàn)過期時間功能
1.存儲時記錄下有效截止時間 2.取數(shù)據(jù)時判斷是否超過有效時間,在有效期內則返回,不在則提示或返回空并且將其刪除 class MyStorage {get(key) {const wrapValue = localStorage.getItem(key)if (wrapValue == null) {return undefined}const {value, expires} = JSON.parse(wrapValue) - if ((expires != null && Date.now() < expires) || expires == null) { + if (!expires || Date.now() < expires) {return value} - + localStorage.rmeoveItem(key)return undefined}set(key, value, period) {const wrapValue = { value };if (period != null) {wrapValue.expires = Date.now() + period;}localStorage.setItem(key, JSON.stringify(wrapValue));} }個人簡介
我是歌謠,歡迎和大家一起交流前后端知識。放棄很容易,
但堅持一定很酷。歡迎發(fā)表自己的看法
主目錄
與歌謠一起通關前端面試題
總結
以上是生活随笔為你收集整理的[js] 模拟 localStorage 时如何实现过期时间功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: adobe reader打开pdf闪退或
- 下一篇: Excel2007版的常用功能(6):E