Java script count down reset problem when user refresh the page

i use a js for count down in my jsp page. when the user refresh the page restart the count down . how to use cookies in my code to prevent restart problem in my code
this is my js code.

var seconds = 130;

function secondPassed() {

	var minutes = Math.round((seconds - 30)/60);
        var ask;

	var remainingSeconds = seconds % 60;

	if (remainingSeconds < 10) {

		remainingSeconds = "0" + remainingSeconds;


	}

	document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;

	if (seconds == 0) {

		clearInterval(countdownTimer);


       ask = window.alert("time up");
       if (ask) {
        window.alert("time up");

        document.location.href = "timeout.jsp";


}
	   document.location.href = "timeout.jsp";	
document.getElementById('countdown').innerHTML = "Buzz Buzz";

	} else {

		seconds--;

	}

}



var countdownTimer = setInterval('secondPassed()', 1000);

plz help me to solve this problem … thanks in advance

Hi gobs,

Welcome to the forums :slight_smile:

Obviously you’re going to need functions to set and read cookies.
I find these quite useful: http://www.quirksmode.org/js/cookies.html

Then you’ll have to read the cookie when the page loads, and if it is present adjust the timer accordingly.
After that you’ll have to set a cookie with the altered value within your call to setInterval.

In psuedo code:

var seconds = readCookie('totalSeconds') || 130;

function secondPassed() {
  ...
}
 
var countdownTimer = setInterval(function(){
  secondPassed();
  if(seconds===0){
    eraseCookie(seconds);
  } else {
    createCookie(seconds , seconds, 7);
  }
}, 1000);

thanks for your reply…
i sovled my problem using your reply. thanks once again