JSFiddle - React, Tailwind, and code Playground
by belorion
JavaScript
// A hash whose values disappear after a while
var FadingHash = (function () {
var fadingHash = function (opts) {
var hash = {};
var timestamp = {};
var lifetime = opts.lifetime || 10 * 1000; // seconds
this.get = function (key) {
if (key in timestamp === false) { return undefined; }
var time = timestamp[key];
var now = new Date();
if (now - time > lifetime) {
delete hash[key];
delete timestamp[key];
}
return hash[key];
};
this.set = function (key, value) {
hash[key] = value;
timestamp[key] = new Date();
};
};
return fadingHash;
})();