我如何使我的jQuery倒计时代码工作

时间:2019-03-23 00:22:33

标签: jquery html

这是我网站的简单jQuery Countdown代码,应该倒计时到2019年4月1日午夜。这是jQuery和HTML代码的相关部分。问题是计时器在运行后不计数。关于以下代码段中的问题和纠正代码的方法的任何建议都将对我有很大帮助。

function makeTimer() {

  var endTime = new Date("01 April 2019 9:56:00 GMT+01:00");			
  endTime = (Date.parse(endTime) / 1000);

  var now = new Date();
  now = (Date.parse(now) / 1000);

  var timeLeft = endTime - now;

  var days = Math.floor(timeLeft / 86400); 
  var hours = Math.floor((timeLeft - (days * 86400)) / 3600);
  var minutes = Math.floor((timeLeft - (days * 86400) - (hours * 3600 )) / 60);
  var seconds = Math.floor((timeLeft - (days * 86400) - (hours * 3600) - (minutes * 60)));

  if (hours < "10") { hours = "0" + hours; }
  if (minutes < "10") { minutes = "0" + minutes; }
  if (seconds < "10") { seconds = "0" + seconds; }

  $(".days").html(days + "<br><span>days</span>");
  $(".hours").html(hours + "<br><span>hours</span>");
  $(".minutes").html(minutes + "<br><span>minutes</span>");
  $(".seconds").html(seconds + "<br><span>seconds</span>");		

}

setInterval(function() { makeTimer(); }, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  <div class="row countdown-box" style="top: 0px">
  <div class="col-md-3">
    <div class="row">
      <div class="col-md-6">
        <h2 id="countdown" class="days">24<br>                        <span>days</span></h2>
      </div>

      <div class="col-md-6">
        <h2 id="countdown">:</h2>
      </div>
    </div>
  </div>

  <div class="col-md-3">
    <div class="row">
      <div class="col-md-6">
        <h2 id="countdown" class="hours">06<br><span>hours</span></h2>
      </div>

      <div class="col-md-6">
        <h2 id="countdown">:</h2>
      </div>
    </div>
  </div>

  <div class="col-md-3">
    <div class="row">
      <div class="col-md-6">
        <h2 id="countdown" class="minutes">22<br><span>minutes</span></h2>
      </div>

      <div class="col-md-6">
        <h2 id="countdown">:</h2>
      </div>
    </div>
  </div>

  <div class="col-md-3">
    <div class="row">
      <div class="col-md-12">
        <h2 id="countdown" class="seconds">59<br><span>seconds</span></h2>
      </div>
    </div>
  </div>
  </div>

2 个答案:

答案 0 :(得分:0)

在HTML中实际上使用ID类型时,您在jquery中传递了.<class>选择器。我建议您改用它:

$("#days").html(days + "<br><span>days</span>");
$("#hours").html(hours + "<br><span>hours</span>");
$("#minutes").html(minutes + "<br><span>minutes</span>");
$("#seconds").html(seconds + "<br><span>seconds</span>");   

答案 1 :(得分:0)

无需在setInterval内创建额外的函数,只需传递函数名

setInterval(makeTimer, 1000)

固定的html

function makeTimer() {

  var endTime = new Date("01 April 2019 9:56:00 GMT+01:00");			
  endTime = (Date.parse(endTime) / 1000);

  var now = new Date();
  now = (Date.parse(now) / 1000);

  var timeLeft = endTime - now;

  var days = Math.floor(timeLeft / 86400); 
  var hours = Math.floor((timeLeft - (days * 86400)) / 3600);
  var minutes = Math.floor((timeLeft - (days * 86400) - (hours * 3600 )) / 60);
  var seconds = Math.floor((timeLeft - (days * 86400) - (hours * 3600) - (minutes * 60)));

  if (hours < "10") { hours = "0" + hours; }
  if (minutes < "10") { minutes = "0" + minutes; }
  if (seconds < "10") { seconds = "0" + seconds; }

  $(".days").html(days + "<br><span>days</span>");
  $(".hours").html(hours + "<br><span>hours</span>");
  $(".minutes").html(minutes + "<br><span>minutes</span>");
  $(".seconds").html(seconds + "<br><span>seconds</span>");		

}

setInterval(makeTimer, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  <div class="row countdown-box" style="top: 0px">
  <div class="col-md-3">
    <div class="row">
      <div class="col-md-6">
        <h2 id="countdown" class="days">24<br>                        <span>days</span></h2>
      </div>

      <div class="col-md-6">
        <h2 id="countdown">:</h2>
      </div>
    </div>
  </div>

  <div class="col-md-3">
    <div class="row">
      <div class="col-md-6">
        <h2 id="countdown" class="hours">06<br><span>hours</span></h2>
      </div>

      <div class="col-md-6">
        <h2 id="countdown">:</h2>
      </div>
    </div>
  </div>

  <div class="col-md-3">
    <div class="row">
      <div class="col-md-6">
        <h2 id="countdown" class="minutes">22<br><span>minutes</span></h2>
      </div>

      <div class="col-md-6">
        <h2 id="countdown">:</h2>
      </div>
    </div>
  </div>

  <div class="col-md-3">
    <div class="row">
      <div class="col-md-12">
        <h2 id="countdown" class="seconds">59<br><span>seconds</span></h2>
      </div>
    </div>
  </div>
  </div>