Skip to content

Instantly share code, notes, and snippets.

@hpbonfim
Created June 10, 2020 13:57
Show Gist options
  • Save hpbonfim/5b23150fe35dcfd4bdc617b117e34510 to your computer and use it in GitHub Desktop.
Save hpbonfim/5b23150fe35dcfd4bdc617b117e34510 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="output"></div>
<script>
var ls2 = {
save: function(key, jsonData) {
if (typeof Storage == "undefined") {
return false;
}
var expirationMS = 8640;
var record = {
value: JSON.stringify(jsonData),
timestamp: new Date().getTime() + expirationMS
};
localStorage.setItem(key, JSON.stringify(record));
return jsonData;
},
load: function(key) {
if (typeof Storage == "undefined") {
return false;
}
var record = JSON.parse(localStorage.getItem(key));
if (!record) {
return false;
}
return (
new Date().getTime() < record.timestamp && JSON.parse(record.value)
);
}
};
// Set Data
ls2.save("a", '{mykey:"myvalue"}', 1200);
ls2.load("a");
// Check Data and output to screen
var checkVal = setInterval(myTimer, 200);
var i = 200;
function myTimer() {
i += 200;
var result = ls2.load("a");
document.getElementById("output").innerHTML +=
i + ": " + result + "<br>";
if (result === false) {
clearInterval(checkVal);
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment