Skip to content

Instantly share code, notes, and snippets.

@SmugZombie
Last active June 28, 2017 20:36
Show Gist options
  • Save SmugZombie/df4213faab7c33bae26b to your computer and use it in GitHub Desktop.
Save SmugZombie/df4213faab7c33bae26b to your computer and use it in GitHub Desktop.
function getLocalStorage(name){
var now = parseInt(new Date() / 1000);
var expires = localStorage.getItem(name+"_expire");
if(!expires){ return localStorage.getItem(name); }
else if(now >= expires){ localStorage.removeItem(name+"_expire"); localStorage.removeItem(name); return ""; }
else{ return localStorage.getItem(name); }
}
function setLocalStorage(name, value, minutes){
if(minutes == null){ localStorage.setItem(name, value); localStorage.removeItem(name+"_expire"); return true} // No set expiration
else if(minutes == 0){ localStorage.removeItem(name); localStorage.removeItem(name+"_expire"); return true} // Setting to 0 kills the localStorage Item any any expiration
else{
var epochExpire = parseInt(new Date() / 1000 + (minutes * 60));
localStorage.setItem(name, value); localStorage.setItem(name+"_expire", epochExpire);
return true
}
return false
}
@SmugZombie
Copy link
Author

Updated to 'STRICT' javascript (IE variables are defined instead of assumed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment