Created
November 26, 2020 02:30
-
-
Save codeyourwayup/98a2190e72e62277d4b2a1447b3f3d50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setWithExpiry(key, value, ttl) { | |
const now = new Date() | |
// `item` is an object which contains the original value | |
// as well as the time when it's supposed to expire | |
const item = { | |
value: value, | |
expiry: now.getTime() + ttl, | |
} | |
localStorage.setItem(key, JSON.stringify(item)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment