We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No description provided.
The text was updated successfully, but these errors were encountered:
可以在存储数据时,在数据中加入一个过期时间戳,然后在获取数据时,先判断该数据的时间戳是否已经过期,如果已过期,则将该数据从 localStorage 中删除,并返回 null,否则返回该数据。
function setWithExpiration(key, value, expirationMinutes) { const expirationMS = expirationMinutes * 60 * 1000; const record = { value: value, timestamp: new Date().getTime() + expirationMS }; localStorage.setItem(key, JSON.stringify(record)); } function getWithExpiration(key) { const record = JSON.parse(localStorage.getItem(key)); if (!record) { return null; } if (new Date().getTime() > record.timestamp) { localStorage.removeItem(key); return null; } return record.value; } // 使用方法 setWithExpiration("data", { foo: "bar" }, 1); // 设置 data 数据,1 分钟后过期 const data = getWithExpiration("data"); // 获取 data 数据 console.log(data); // { foo: "bar" } setTimeout(() => { const expiredData = getWithExpiration("data"); // 获取 data 数据 console.log(expiredData); // null,数据已过期,被自动删除 }, 60 * 1000); // 1 分钟后
Sorry, something went wrong.
function setWithExpiration(key, value, expirationMinutes) { const expirationsMS = expirationMinutes * 60 * 1000 const record = { value: value, timestamp: new Date().getTime() + expirationsMS, } localStorage.setItem(key, JSON.stringify(record)) } function getWithExpiration(key) { const record = JSON.parse(localStorage.getItem(key)) if (!record) { return null } if (new Date().getTime() >= record.timestamp) { localStorage.removeItem('key') return null } else { return record } } setWithExpiration('a', '123', 1 / 20)
No branches or pull requests
No description provided.
The text was updated successfully, but these errors were encountered: