Skip to content

Instantly share code, notes, and snippets.

@codeyourwayup
Created November 26, 2020 02:30
Show Gist options
  • Save codeyourwayup/38336ed4773b8f3b81a91387434a142e to your computer and use it in GitHub Desktop.
Save codeyourwayup/38336ed4773b8f3b81a91387434a142e to your computer and use it in GitHub Desktop.
function getWithExpiry(key) {
const itemStr = localStorage.getItem(key)
// if the item doesn't exist, return null
if (!itemStr) {
return null
}
const item = JSON.parse(itemStr)
const now = new Date()
// compare the expiry time of the item with the current time
if (now.getTime() > item.expiry) {
// If the item is expired, delete the item from storage
// and return null
localStorage.removeItem(key)
return null
}
return item.value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment