Skip to content

Instantly share code, notes, and snippets.

@soarez
Created August 7, 2014 22:50
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 soarez/73c896046d518475b55d to your computer and use it in GitHub Desktop.
Save soarez/73c896046d518475b55d to your computer and use it in GitHub Desktop.
Dickson storage
(function() {
var ds = window.dicksonStorage = { };
var data = getData();
var INTERVAL = 400;
setInterval(ianWorker, INTERVAL);
ds.clear = clear;
function clear() {
data = {};
setData();
}
ds.getItem = getItem;
function getItem(key) {
return data[key] === undefined ? null : data[key];
}
ds.setItem = setItem;
function setItem(key, value) {
data[key] = value;
setData();
}
ds.removeItem = removeItem;
function removeItem(key) {
delete data[key];
setData();
}
function getData() {
return JSON.parse(localStorage.dicksonStorage || '{}');
}
function setData() {
localStorage.dicksonStorage = JSON.stringify(data);
}
function ianWorker() {
if (Date.now() - localStorage.dicksonTicket > INTERVAL * 2)
clear();
localStorage.dicksonTicket = Date.now();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment