function getDatas() {
let cacheKey = 'memories';
if (cacheKey in localStorage) {
let datas = JSON.parse(localStorage.getItem(cacheKey));
// if expired
if (datas['expires'] < Date.now()) {
localStorage.removeItem(cacheKey);
getDatas()
} else {
setDatas(datas);
}
} else {
$.ajax({
"dataType": "json",
"success": function(datas, textStatus, jqXHR) {
let today = new Date();
datas['expires'] = today.setDate(today.getDate() + 7) // expires in next 7 days
setDatas(datas);
localStorage.setItem(cacheKey, JSON.stringify(datas));
},
"url": "http://localhost/phunsanit/snippets/PHP/json.json_encode.php",
});
}
}
function setDatas(datas) {
// display json as text
$('#datasA').text(JSON.stringify(datas));
// your code here
....
}
// call
getDatas();
enter link description here