I am using localstorage
and getTime
, to receive the time and save it in localstorage
and then calculate if 1 minuto
has passed and update localstorage
and display a text in div
. (I clarify that this is fake, hence the Math random and localstorage)
The problem I have is that when updating the page does not keep the text, I want to keep the text and that when the 60 seconds pass, it is simply updated (in this case when the person reloads the page) the changes.
function hasOneDayPassed(){
var lastclear = localStorage.getItem('lastclear'),
time_now = (new Date()).getTime();
// .getTime() returns milliseconds so 1000 * 60 = 60 sec
if ((time_now - lastclear) > 1000 * 60) {
localStorage.clear();
localStorage.setItem('lastclear', time_now);
$('#now-people').text( Math.floor(Math.random() * 20) + ' people are looking right now');
}
}
hasOneDayPassed(); // run the code