Skip to content

Instantly share code, notes, and snippets.

@liesislukas
Last active January 5, 2016 19:53
Show Gist options
  • Save liesislukas/f038d7c3c90a7c5b896f to your computer and use it in GitHub Desktop.
Save liesislukas/f038d7c3c90a7c5b896f to your computer and use it in GitHub Desktop.
var ls = {
set: function (variable, value, ttl_ms) {
var data = {value: value, expires_at: new Date().getTime() + ttl_ms / 1};
localStorage.setItem(variable.toString(), JSON.stringify(data));
},
get: function (variable) {
var data = JSON.parse(localStorage.getItem(variable.toString()));
if (data !== null) {
if (data.expires_at !== null && data.expires_at < new Date().getTime()) {
localStorage.removeItem(variable.toString());
} else {
return data.value;
}
}
return null;
}
};
module.exports = ls;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment