倒数计时器正在重新启动,我点击刷新

时间:2017-09-28 13:58:21

标签: javascript jquery css

每当我刷新浏览器时,它都会从头开始重新启动。我把它上传到服务器上......我得到了相同的结果。 这是我的代码:



function getTimeRemaining(endtime) {
  var t = Date.parse(endtime) - Date.parse(new Date());
  var seconds = Math.floor((t / 1000) % 60);
  var minutes = Math.floor((t / 1000 / 60) % 60);
  var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
  var days = Math.floor(t / (1000 * 60 * 60 * 24));
  return {
    'total': t,
    'days': days,
    'hours': hours,
    'minutes': minutes,
    'seconds': seconds
  };
}

function initializeClock(id, endtime) {
  var clock = document.getElementById(id);
  var daysSpan = clock.querySelector('.days');
  var hoursSpan = clock.querySelector('.hours');
  var minutesSpan = clock.querySelector('.minutes');
  var secondsSpan = clock.querySelector('.seconds');

  function updateClock() {
    var t = getTimeRemaining(endtime);

    daysSpan.innerHTML = t.days;
    hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
    minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
    secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);

    if (t.total <= 0) {
      clearInterval(timeinterval);
    }
  }

  updateClock();
  var timeinterval = setInterval(updateClock, 1000);
}

var deadline = new Date(Date.parse(new Date()) + 33 * 24 * 60 * 60 * 1000);
initializeClock('clockdiv', deadline);
&#13;
#clockdiv {
  font-family: Calibri;
  color: #001a00;
  display: inline-block;
  font-weight: 10;
  text-align: center;
  font-size: 30px;
}

#clockdiv>div {
  padding: 10px;
  border-radius: 3px;
  display: inline-block;
}

.smalltext {
  padding-top: 5px;
  font-size: 16px;
}
&#13;
<div id="clockdiv">
  <div>
    <span class="days"></span>
    <div class="smalltext">Days</div>
  </div>
  <div>
    <span class="hours"></span>
    <div class="smalltext">Hours</div>
  </div>
  <div>
    <span class="minutes"></span>
    <div class="smalltext">Minutes</div>
  </div>
  <div>
    <span class="seconds"></span>
    <div class="smalltext">Seconds</div>
  </div>
</div>
&#13;
&#13;
&#13;

这里是小提琴 - code

我在这里做错了什么? 起初我觉得如果我把它上传到服务器上就好了。但仍然得到相同的结果。 如果有另外的代码..请帮助我或指导我..如何做到这一点! 谢谢!

2 个答案:

答案 0 :(得分:2)

正是因为这条线

var deadline = new Date(Date.parse(new Date()) + 33 * 24 * 60 * 60 * 1000);

您将来的日期设置为33天,因此每次加载页面都会设置。

如果您将此行更改为以下内容并传入所需的日期。

var deadline = new Date(Date.parse('2017-10-30'));

在下面运行代码段。

&#13;
&#13;
function getTimeRemaining(endtime) {
  var t = Date.parse(endtime) - Date.parse(new Date());
  var seconds = Math.floor((t / 1000) % 60);
  var minutes = Math.floor((t / 1000 / 60) % 60);
  var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
  var days = Math.floor(t / (1000 * 60 * 60 * 24));
  return {
    'total': t,
    'days': days,
    'hours': hours,
    'minutes': minutes,
    'seconds': seconds
  };
}

function initializeClock(id, endtime) {
  var clock = document.getElementById(id);
  var daysSpan = clock.querySelector('.days');
  var hoursSpan = clock.querySelector('.hours');
  var minutesSpan = clock.querySelector('.minutes');
  var secondsSpan = clock.querySelector('.seconds');

  function updateClock() {
    var t = getTimeRemaining(endtime);

    daysSpan.innerHTML = t.days;
    hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
    minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
    secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);

    if (t.total <= 0) {
      clearInterval(timeinterval);
    }
  }

  updateClock();
  var timeinterval = setInterval(updateClock, 1000);
}

var deadline = new Date(Date.parse('2017-10-30'));
initializeClock('clockdiv', deadline);
&#13;
#clockdiv {
  font-family: Calibri;
  color: #001a00;
  display: inline-block;
  font-weight: 10;
  text-align: center;
  font-size: 30px;
}

#clockdiv>div {
  padding: 10px;
  border-radius: 3px;
  display: inline-block;
}

.smalltext {
  padding-top: 5px;
  font-size: 16px;
}
&#13;
<div id="clockdiv">
  <div>
    <span class="days"></span>
    <div class="smalltext">Days</div>
  </div>
  <div>
    <span class="hours"></span>
    <div class="smalltext">Hours</div>
  </div>
  <div>
    <span class="minutes"></span>
    <div class="smalltext">Minutes</div>
  </div>
  <div>
    <span class="seconds"></span>
    <div class="smalltext">Seconds</div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

设置

var deadline = new Date(Date.parse(new Date()) + 33 * 24 * 60 * 60 * 1000);

每次加载脚本时,从现在起它将始终为33天(依此类推)。所以你必须给脚本一个固定的截止日期:

var deadline = new Date(Date.parse('2017-10-12'));