Question

Caching a widget? (Keep a countdown timer from restarting on refresh)

  • 18 August 2021
  • 2 replies
  • 95 views

Hello, I have a small problem, I made a script that shows a countdown from 30 minutes down to 0, however when I refresh the page it (obviously) restart from 30 minutes.

I’d like for the timer to keep running even if the page refreshes. I think I have to do something about caching it but I’m not sure how to go about it.


2 replies

Userlevel 6
Badge +1

Maybe share your code so we may see what you’ve got and what needs to be amended/added to achieve your desired outcome.

You are right, here’s the code

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>


<script>
var adsa = 1800;
var minutes = Math.floor(adsa / 60);
var seconds = adsa - minutes * 60;
var zero;

function myFunction() {
minutes = Math.floor(adsa / 60);
seconds = adsa - minutes * 60;
if (seconds-10 < 0) {

zero = "0";
} else zero = "";

if (adsa > 0) {
document.getElementById("demo").innerHTML = "xxxx" + minutes + ":" + zero + seconds + "</b> xxxxx <b>xxxxx !</b>";
adsa = adsa-1;

}

}
setInterval(
myFunction, 1000)
</script>

</body>
</html>

It’s inside a “HTML code” widget

Reply