JSFiddle - React, Tailwind, and code Playground
JavaScript
window.Cache = {
Get: function (name, call, time) {
var st = this.Storage();
var data = st.getItem(name);
if (data != undefined) {
data = JSON.parse(data);
var times = parseInt(new Date().getTime() / 1000) - data.time;
alert(times);
if (times >= time) { // не срабатывает
alert('cache');
return data.data;
} else {
st.removeItem(name);
var gg = call();
st.setItem(name, JSON.stringify({
data: JSON.stringify(gg),
time: parseInt(new Date().getTime() / 1000)
}));
return gg;
}
} else {
var gg = call();
st.setItem(name, JSON.stringify({
data: JSON.stringify(gg),
time: parseInt(new Date().getTime() / 1000)
}));
return gg;
}
},
Set: function (name, value) {
var st = this.Storage();
st.setItem(name, JSON.stringify({
data: JSON.stringify(value),
time: parseInt(new Date().getTime() / 1000)
}));
},
Del: function (name) {
var st = this.Storage();
st.removeItem(name);
},
Clear: function () {
var st = this.Storage();
st.clear();
},
/**
* Создаёт обьект localStorage. Если произошла ошибка емулирует localStorage.
* (все данные хранит в озу, пропадают при перегрузке страницы).
*/
Storage: function () {
try {
return window['localStorage'];
} catch (e) {
return {
storage: {},
clear: function () {
delete this.storage;
this.storage = {};
},
getItem: function (key) {
return this.storage[key];
},
removeItem: function (key)...