SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.
    Code JavaScript:
    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
    Last edited by Mittineague; Oct 1, 2013 at 23:13. Reason: reformatting bbcode tags

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,051
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi gobs,

    Welcome to the forums

    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:

    Code:
    var seconds = readCookie('totalSeconds') || 130;
    
    function secondPassed() {
      ...
    }
     
    var countdownTimer = setInterval(function(){
      secondPassed();
      if(seconds===0){
        eraseCookie(seconds);
      } else {
        createCookie(seconds , seconds, 7);
      }
    }, 1000);

  3. #3
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    thanks.

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •