const localStorage = (function () {
let store = {}
return {
getItem: function (key) {
return store[key] || null
},
setItem: function (key, val, time) {
time = Number(time) || 0;
store[key] = val.toString();
if (time > 0) {
this.timeOut(key, time);
}
},
timeOut: function (key, time) {
var timer = setTimeout(() => {
this.removeItem(key);
clearTimeout(timer)
}, time);
},
removeItem: function (key) {
delete store[key]
},
clear: function () {
store = {}
}
}
})()
Object.defineProperty(window, 'localStorage2', {
value: localStorage
})