【问题标题】:Javascript countdown time incorrectJavascript倒计时时间不正确
【发布时间】:2020-01-10 13:26:08
【问题描述】:

我对 Jquery 倒数计时器有疑问。脚本每天倒计时到同一时间。 倒计时时间不对,倒计时一小时。任何想法如何解决这一问题?它托管在 JSFiddle 上。

(function($){
  var date    = new Date(),
      month   = date.getMonth();
      day     = date.getDate(),
      weekDay = date.getDay(),
      hours   = {
        start: new Date(date.getFullYear(), month, day),
        end: new Date(date.getFullYear(), month, day)
      };
  
  // weekDay var [0 = sun, 1 = mon, 2 = tues ... 5 = fri 6 = sat]
  
  // If it's Monday - Friday
  if(weekDay >= 1 && weekDay <= 5){

    // Start at 7am, end at 8pm
    hours.start.setHours(7);
    hours.end.setHours(22);

  // If it's Saturday
  } else if(weekDay == 6){

    // Start at 8am, end at 8pm
    hours.start.setHours(8);
    hours.end.setHours(20);

  // If it's Sunday
  } else {

    // Start at 9am, end at 6pm
    hours.start.setHours(9);
    hours.end.setHours(18);
  }

  function countDown(){
    var date         = new Date(),
        countHours   = ('0' + (hours.end.getHours() - date.getHours())).substr(-2),
        countMinutes = ('0' + (59 - date.getMinutes())).substr(-2),
        countSeconds = ('0' + (59 - date.getSeconds())).substr(-2);

    // If it's currently not within the hours, don't show the countdown
    if(date.getHours() < hours.start.getHours() || date.getHours() > hours.end.getHours()){
      $('.countdown').hide();
    } else if($('.countdown').not(':visible')){
      $('.countdown').show();
    }

    $('.countdown .hours').text(countHours);
    $('.countdown .minutes').text(countMinutes);
    $('.countdown .seconds').html(countSeconds);

  }

  $(function(){
    setInterval(function(){
      countDown();
    }, 1000);
  });
})(jQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="countdown">
<span class="hours"></span>H <span class="minutes"></span>M <span class="seconds"></span>S
</div>

JSFiddle:http://jsfiddle.net/chrisoliver1990/s1nzydfj/36/

【问题讨论】:

  • 在计算小时数时,您没有考虑 分钟。分钟和秒也会发生同样的事情。

标签: javascript jquery countdown


【解决方案1】:

问题在于您没有正确计算小时和分钟。假设你想在 22:00 完成倒计时,而现在是 10:30,那么这是 11 小时 30 分钟。

但是,您的计算只执行hours.end.getHours() - date.getHours(),使用上面的值得出22 - 10。这是12,而不是11,因为代码没有考虑已经过去的半小时。

同样的问题出现在会议记录中,但不太明显。尽管如此,分钟显示还是关闭了 1,因为它没有考虑已经过去的秒数。

相反,您应该采用不同的方法并获得总差异以秒为单位,然后计算其代表的小时、分钟和秒:

var end = new Date('2020-01-10T22:00:00');

//let's fake it to 10:30:42
var current = new Date('2020-01-10T10:30:42');

//getTime returns milliseconds, divide by 1000 to get seconds
var differenceInSeconds = Math.floor((end.getTime() - current.getTime()) / 1000);

//for convenience
var secondsInHour = 60 * 60;
var secondsInMinute = 60;


var hours = Math.floor(differenceInSeconds / secondsInHour);

//take the hours out of the difference
differenceInSeconds -= hours * secondsInHour;

var minutes = Math.floor(differenceInSeconds / secondsInMinute);

//take the minutes out of the difference
differenceInSeconds -= minutes * secondsInMinute;

//the rest is just the seconds left
var seconds = differenceInSeconds;

console.log(hours, minutes, seconds);

将此时间计算添加到您的代码中可以正确计算剩余时间:

(function($){
  var date    = new Date(),
      month   = date.getMonth();
      day     = date.getDate(),
      weekDay = date.getDay(),
      hours   = {
        start: new Date(date.getFullYear(), month, day),
        end: new Date(date.getFullYear(), month, day)
      };
  
  // weekDay var [0 = sun, 1 = mon, 2 = tues ... 5 = fri 6 = sat]
  
  // If it's Monday - Friday
  if(weekDay >= 1 && weekDay <= 5){

    // Start at 7am, end at 8pm
    hours.start.setHours(7);
    hours.end.setHours(22);

  // If it's Saturday
  } else if(weekDay == 6){

    // Start at 8am, end at 8pm
    hours.start.setHours(8);
    hours.end.setHours(20);

  // If it's Sunday
  } else {

    // Start at 9am, end at 6pm
    hours.start.setHours(9);
    hours.end.setHours(18);
  }
  
  function formatNumber(num) {
    return ('0' + num).substr(-2);
  }
  
  function countDown(){
    var date         = new Date();
    
    var end = new Date('2020-01-10T22:00:00'),
        differenceInSeconds = Math.floor((hours.end.getTime() - date.getTime()) / 1000);

    //for convenience
    var secondsInHour = 60 * 60;
    var secondsInMinute = 60;

    var countHours = formatNumber(Math.floor(differenceInSeconds / secondsInHour));

    differenceInSeconds -= countHours * secondsInHour;

    var countMinutes = formatNumber(Math.floor(differenceInSeconds / secondsInMinute));

    differenceInSeconds -= countMinutes * secondsInMinute;
    
    var countSeconds = formatNumber(differenceInSeconds);

    // If it's currently not within the hours, don't show the countdown
    if(date.getHours() < hours.start.getHours() || date.getHours() > hours.end.getHours()){
      $('.countdown').hide();
    } else if($('.countdown').not(':visible')){
      $('.countdown').show();
    }

    $('.countdown .hours').text(countHours);
    $('.countdown .minutes').text(countMinutes);
    $('.countdown .seconds').html(countSeconds);

  }

  $(function(){
    setInterval(function(){
      countDown();
    }, 1000);
  });
})(jQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="countdown">
<span class="hours"></span>H <span class="minutes"></span>M <span class="seconds"></span>S
</div>

(我还引入了一个辅助函数formatNumber来封装数字前是否显示零的逻辑)

【讨论】:

    【解决方案2】:

    setHourshoursValue 参数是基于 0 的,所以当你希望它是晚上 8 点时,你将它设置为 19(而不是 20)。检查 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours

    `

        (function($){
          var date    = new Date(),
              month   = date.getMonth();
              day     = date.getDate(),
              weekDay = date.getDay(),
              hours   = {
                start: new Date(date.getFullYear(), month, day),
                end: new Date(date.getFullYear(), month, day)
              };
          
          // weekDay var [0 = sun, 1 = mon, 2 = tues ... 5 = fri 6 = sat]
          
          // If it's Monday - Friday
          if(weekDay >= 1 && weekDay <= 5){
    
            // Start at 7am, end at 8pm
            hours.start.setHours(7);
            hours.end.setHours(19);
    
          // If it's Saturday
          } else if(weekDay == 6){
    
            // Start at 8am, end at 8pm
            hours.start.setHours(8);
            hours.end.setHours(20);
    
          // If it's Sunday
          } else {
    
            // Start at 9am, end at 6pm
            hours.start.setHours(9);
            hours.end.setHours(18);
          }
    
          function countDown(){
            var date         = new Date(),
                countHours   = ('0' + (hours.end.getHours() - date.getHours())).substr(-2),
                countMinutes = ('0' + (59 - date.getMinutes())).substr(-2),
                countSeconds = ('0' + (59 - date.getSeconds())).substr(-2);
    
            // If it's currently not within the hours, don't show the countdown
            if(date.getHours() < hours.start.getHours() || date.getHours() > hours.end.getHours()){
              $('.countdown').hide();
            } else if($('.countdown').not(':visible')){
              $('.countdown').show();
            }
    
            $('.countdown .hours').text(countHours);
            $('.countdown .minutes').text(countMinutes);
            $('.countdown .seconds').html(countSeconds);
    
          }
    
          $(function(){
            setInterval(function(){
              countDown();
            }, 1000);
          });
        })(jQuery);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="countdown">
        <span class="hours"></span>H <span class="minutes"></span>M <span class="seconds"></span>S
        </div>

    【讨论】:

    • "setHours hoursValue 参数是从 0 开始的,所以当你希望它是晚上 8 点时,你将它设置为 19(而不是 20)" 这是完全错误的。一天的开始时间是 00:00,晚上 8 点是 20:00。晚上 8 点正好是一天开始后的 20 小时:0 到 12 点是 12 小时,再加上 8 点就是 20 小时。
    • 这在MDN中有提到。请参考我分享的链接。来自 MDN - hoursValue Ideally, an integer between 0 and 23, representing the hour. If a value greater than 23 is provided, the datetime will be incremented by the extra hours.
    • 当然,并且与其他 24 小时制一样 - 0 是午夜,1 = 1AM,2 = 2AM,3 = 3AM等,直到12 12PM,然后你有13 = 1PM,14 = 2PM,15 = 3PM,16 = 4PM,17 = 5 PM,18 =下午 6 点,19 = 7 点,20 = 8 点,21 = 9 点,22 = 10 点,23 = 11 点,然后它环绕到0 是午夜。什么时候晚上 8 点变成 19 点?为什么你坚持a 24 hour time format 的工作方式与实际不同?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多