简体   繁体   English

我使用了倒计时器,但它自行重置

[英]I have used a count down timer, but it resets itself

Whenever i refresh the page the timer resets. 每当我刷新页面时,计时器都会重置。 It starts from from the begining. 它从一开始就开始。 I have use the plugin called countdown.js 我使用了名为countdown.js的插件

Here is the code : 这是代码:

<script type="text/javascript">
  var now = new Date();
    var countTo = 30 * 24 * 60 * 60 * 1000 + now.valueOf();
    $('.timer').countdown(countTo, function(event) {
        var $this = $(this);
        switch(event.type) {
            case "seconds":
            case "minutes":
            case "hours":
            case "days":
            case "weeks":
            case "daysLeft":
                $this.find('span.'+event.type).html(event.value);
                break;
            case "finished":
                $this.hide();
                break;
        }
    });
</script>


<div class="col-md-8 col-md-offset-3 timer">
    <div class="days-wrapper" style="text-align:center;">
        <span class="days"></span> 
        <p style="margin-left:-80px;"><br><br>DAYS</p>
    </div>
    <div class="hours-wrapper" style="text-align:center;">
        <span class="hours"></span> 
        <p style="margin-left:-80px;"><br><br>HOURS</p>
    </div>
    <div class="minutes-wrapper" style="text-align:center;">
        <span class="minutes"></span> 
        <p style="margin-left:-80px;"><br><br>MIN</p>
    </div>
</div>

Everytime you reload a page javascripts "forgots" whatever you have done previously and your code is executed entirely from the beginning. 每当你重新加载页面javascripts“forgots”时,无论你以前做过什么,你的代码都是从头开始执行的。

A possible solution is to use some persistent storage method such as localStorage 一种可能的解决方案是使用一些持久存储方法,例如localStorage

In your example, you can do something like: 在您的示例中,您可以执行以下操作:

var previousCountTo = localStorage.getItem('time'); 
var now = new Date(); 
var countTo = previousCountTo?parseInt(previousCountTo, 10):(30 * 24 * 60 * 60 * 1000 + now.valueOf());
localStorage.setItem('time', countTo); // Save the current time.
$('.timer').countdown(countTo, function(event) {
      var $this = $(this);
    console.log(event);
      switch(event.type) {
          case "seconds":
          case "minutes":
          case "hours":
          case "days":
          case "weeks":
          case "daysLeft":
              $this.find('span.'+event.type).html(event.value);
              break;
          case "finished":
              $this.hide();
              localStorage.removeItem('time'); // once finished, remove the timer.
              break;
      }
});

When JavaScript is executed inside a browser, its execution scope is limited to the lifetime of the loaded page. 当JavaScript在浏览器中执行时,其执行范围仅限于加载页面的生命周期。

After the page is unloaded, all the state is discarded, which includes variables and timers. 卸载页面后,将丢弃所有状态,包括变量和计时器。 So the answer is you cannot do that . 所以答案是你不能那样做

PS: You cannot preserve variable state after the page is unloaded. PS:卸载页面后无法保留变量状态。 This does not mean you can get no state over the edge. 并不意味着你可以在边缘得不到状态。 See @tehsis's excellent answer . 请参阅@ tehsis的优秀答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM