2021-08-02 Javascript localStorage 1. Set expiry time(TTL) for LocalStorage1234567891011function setWithExpiry(key, value, ttl){ const new = new Date() const item = { value : value expiry : new.getTime() + ttl } localstorage.setItem(key, JSON.stringify(item))} 2. Get items from LocalStorage123456789101112131415function getWithExpiry(key){ const itemStr = localstorage.getItem(key) if(!itemStr){ return null } const item = JSON.parse(itemStr) const now = new Date() if( now.getTime() > item.expiry ){ localStorage.removeItem(key) } return item.value} Newer fetchApi Older 组合模式