Skip to content

Instantly share code, notes, and snippets.

@jakeNiemiec
Forked from anhang/localStorage.js
Created March 9, 2017 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakeNiemiec/6fcdf65944be08da30210179e653adc8 to your computer and use it in GitHub Desktop.
Save jakeNiemiec/6fcdf65944be08da30210179e653adc8 to your computer and use it in GitHub Desktop.
HTML5 Local Storage with Expiration
AZHU.storage = {
save : function(key, jsonData, expirationMin){
if (!Modernizr.localstorage){return false;}
var expirationMS = expirationMin * 60 * 1000;
var record = {value: JSON.stringify(jsonData), timestamp: new Date().getTime() + expirationMS}
localStorage.setItem(key, JSON.stringify(record));
return jsonData;
},
load : function(key){
if (!Modernizr.localstorage){return false;}
var record = JSON.parse(localStorage.getItem(key));
if (!record){return false;}
return (new Date().getTime() < record.timestamp && JSON.parse(record.value));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment