Skip to content

Instantly share code, notes, and snippets.

@cyio
Forked from anhang/localStorage.js
Last active September 20, 2016 10:56
Show Gist options
  • Save cyio/25e97f117e3bd6d95535c89016a58cee to your computer and use it in GitHub Desktop.
Save cyio/25e97f117e3bd6d95535c89016a58cee 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;}
// 没有超期返回保存的数据,如果超了返回false
return (new Date().getTime() < record.timestamp && JSON.parse(record.value));
}
}
@cyio
Copy link
Author

cyio commented Sep 20, 2016

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