Skip to content

Instantly share code, notes, and snippets.

@kulerbox
Last active February 10, 2016 16:57
Show Gist options
  • Save kulerbox/f59a08789287444f5546 to your computer and use it in GitHub Desktop.
Save kulerbox/f59a08789287444f5546 to your computer and use it in GitHub Desktop.
var i = 0;
function setNewVal(prop) {
window.localStorage[prop + "timestamp"] = new Date();
//console.log(localStorage.sectimestamp + ' 1');
}
function recursive() {
for (var prop in window.localStorage) {
//if the property name contains the string "timestamp"
if (prop.indexOf("timestamp") != -1) {
//get stored timestamp val
var timestamp = new Date(window.localStorage[prop]);
var currentTime = new Date();
//set maxAge
var maxAge = (1000 * 10); //s
console.log(currentTime - timestamp > maxAge);
if ((currentTime - timestamp) > maxAge) {
//if the property is too old
//increment counter
i++
//get the string of the real property (this prop - "timestamp")
var propString = prop.replace("timestamp", "");
//send it to function that sets a new value
setNewVal(propString);
}
}
}
}
//init storage
setNewVal('timeVault');
//run check continueosly
setInterval(recursive, 1000);
//localStorage.clear();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment