Click here to Skip to main content
15,393,863 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have a countdown timer,which has the span of 15 minutes once it end a new countdown of 15 minute will start.
the problem is that when ever i refresh page it also get refreshed.so kindly tell me how to make it universal.

What I have tried:

<script type = "text/javascript">

var timeInSecs;
var ticker;

function startTimer(secs) {
timeInSecs = parseInt(secs);
ticker = setInterval("tick()", 1000);
}

function tick() {
var secs = timeInSecs;
if (secs > 0) {
timeInSecs--;
}
else {
clearInterval(ticker);
startTimer(900); // start again
}

var hours = Math.floor(secs / 3600);
secs %= 3600;
var mins = Math.floor(secs / 60);
secs %= 60;
var pretty = ((hours < 10) ? "0" : "") + hours + ":" + ((mins < 10) ? "0" : "") + mins + ":" + ((secs < 10) ? "0" : "") + secs;
document.getElementById("countdown").innerHTML = pretty;
}

startTimer(900); // 15 minutes in seconds

</script>
Posted
Updated 22-Aug-16 1:56am
Comments
Karthik_Mahalingam 21-Aug-16 11:59am
   
try using cookies or localstorage
Ralf Meier 21-Aug-16 14:53pm
   
Disconnect it from the page and connect it to a base-part of your application.
Shantanu sinha 21-Aug-16 15:26pm
   
how to go for it.
Philippe Mori 21-Aug-16 18:40pm
   
Use code block for your code to make it readable.

1 solution

As mentioned in comments, you'll have to store the value somewhere so that when the page loads you can check if the timer is currently running and then code from there.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900