Im making a web game.
I want to have a cooldown on some actions, but the problem now is that if you refresh the page the cooldown is away. I know i could get the date, and then store the date in localstorage. But i cant figure it out.
function cooldown(){
var timeleft = 0;
timeleft = document.getElementById('lagre02').textContent;
timeleft = parseInt (timeleft, 10);
localStorage.setItem("tid", timeleft);
timeleft = localStorage.getItem("tid");
if (timeleft > 0) {
var myNode = document.getElementById("info01");
while (myNode.firstChild) {
myNode.removeChild(myNode.lastChild);
}
document.getElementById("info01").innerHTML = "<div class='d-flex flex-column justify-content-center align-items-center w-100 h-100'><p id='p_01'>Du har fortsett:</p> <br> <p class='text-center fs-3' id='countdown'></p> <br> <p id='p_01'>sekunder igjen.</p></div>"
var timer = setInterval(function(){
if(timeleft <= 0){
clearInterval(timer);
location.reload();
} else {
document.getElementById("countdown").innerHTML = timeleft;
}
timeleft -= 1;
}, 1000);
}
}
The var "timeleft" is set when the action is complete.
I think the best soulution is to use server side date, but i cant figure it out.
Aucun commentaire:
Enregistrer un commentaire