Created
November 26, 2020 02:30
-
-
Save codeyourwayup/38336ed4773b8f3b81a91387434a142e 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 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