Last active
June 28, 2017 20:36
-
-
Save SmugZombie/df4213faab7c33bae26b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to 'STRICT' javascript (IE variables are defined instead of assumed)