Domanda

When i refresh the browser, the timer resets, so how to make it not reset? This is my code. Please check it.

<?php echo $waktune; ?> // You can change it into seconds
var detik = <?php echo $waktune; ?>;
if (document.images) {
    parselimit = detik
}
function begintimer() {
    if (!document.images)
        return
    if (parselimit < 12) {
        document.getElementById("servertime").style.color = "Green";
    }
    if (parselimit == 1) {
        document.getElementById("hasil").submit();
    } else {
        parselimit -= 1 curmin = Math.floor(parselimit / 60)
            cursec = parselimit % 60
        if (curmin != 0)
            curtime = curmin + ":" + cursec + ""else
            curtime = cursec + " detik"document.getElementById("servertime").innerHTML = curtime setTimeout("begintimer()", 1000)
        }
}
È stato utile?

Soluzione

Try to use session storage :

// Store
sessionStorage.setItem("key", "value");
// Retrieve
document.getElementById("result").innerHTML=sessionStorage.getItem("key"); 

Update

Example :

<head>

</head>
<body>
    <div id="divCounter"></div>
    <script type="text/javascript">
    if (sessionStorage.getItem("counter")) {
      if (sessionStorage.getItem("counter") >= 10) {
        var value = 0;
      } else {
        var value = sessionStorage.getItem("counter");
      }
    } else {
      var value = 0;
    }
    document.getElementById('divCounter').innerHTML = value;

    var counter = function () {
      if (value >= 10) {
        sessionStorage.setItem("counter", 0);
        value = 0;
      } else {
        value = parseInt(value) + 1;
        sessionStorage.setItem("counter", value);
      }
      document.getElementById('divCounter').innerHTML = value;
    };

    var interval = setInterval(counter, 1000);
  </script>
</body>

Altri suggerimenti

Store the server time in a cookie (see setcookie) and load that. You'll want to think about how long you want this cookie to last though.

You can use local storage, such as :

localStorage.setItem('countDownValue', curtime); // To set the value
...
curtime = localStorage.getItem('countDownValue'); // To get the value

I think you have to save some value in the cookie and reset timer only if timer > x && cookie is already been setted.

Set cookie on init:

setcookie("reloaded","true");

Set cookie on reaload:

setcookie("reloaded","false");

Check:

if($_COOKIE["reloaded"] == false && timer > $time) {
   /* reset timer */
}
<form name="counter">
    <input type="text" size="8" name="chandresh" id="counter">
</form>

<script type="text/javascript">
function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

var cnt = 60;
function counter(){
    if(getCookie("cnt") > 0){
        cnt = getCookie("cnt");
    }
    cnt -= 1;
    document.cookie = "cnt="+ cnt;
    jQuery("#counter").val(getCookie("cnt"));

    if(cnt>0){
        setTimeout(counter,1000);
    }

}

counter();
</script>

Source: http://chandreshrana.blogspot.in/2017/01/how-to-make-counter-not-reset-on-page.html

How to create a countdown timer with JavaScript.

var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get today's date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Output the result in an element with id="demo"
  document.getElementById("demo").innerHTML = hours + "h "
  + minutes + "m " + seconds + "s ";

  // If the count down is over, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);

Try it yourself

Hope for help.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top