Skip to content

Instantly share code, notes, and snippets.

@codeyourwayup
Created November 26, 2020 02:30
Show Gist options
  • Save codeyourwayup/98a2190e72e62277d4b2a1447b3f3d50 to your computer and use it in GitHub Desktop.
Save codeyourwayup/98a2190e72e62277d4b2a1447b3f3d50 to your computer and use it in GitHub Desktop.
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