Jquery倒计时一天

时间:2014-12-03 04:04:11

标签: jquery

我有一个倒计时jquery工作小时,分钟和秒。到目前为止,这完美地运作。不过我想要它也算上一天

这是JS FIDDLE

HTML

<div id="counttime">24:00:00</div>
<div id="cons"></div>

JQUERY

    function pad(num) {
  return num<10?"0"+num:num;
}
$(function() {
  var tCont = $('#counttime');
  var timer = tCont.html().split(':');

  if (timer.length === 2) timer.unshift(0);
  var endMilli= timer[0]*60*60*1000;
  endMilli += timer[1]*60*1000
  endMilli += timer[2]*1000;
  if(endMilli > 0) {
    var endTime = new Date(Date.now()+endMilli);   
$("#cons").append("<br>"+endTime);
    var tId = setInterval(function() {
      var diff = endTime.getTime()-Date.now();
      if (diff<=0) {
        tCont.html("00:00:00");
        clearInterval(tId);
      }
      else {
        var d = new Date(diff);
        hh = pad(d.getUTCHours()); 
        mm = pad(d.getMinutes());
        ss = pad(d.getSeconds());
        tCont.html(""+hh+":"+mm+":"+ss);
      }
    },300);      
  }
});

2 个答案:

答案 0 :(得分:1)

您必须调整代码以允许4个部分,然后添加一行来评估日期:

dd = pad( (diff - diff % (864e5))/864e5 ),

DEMO

答案 1 :(得分:0)

您的hh部分应该乘以天数!

hh = pad(d.getUTCHours() + d.getDay()*24); 

您还应该添加月份......不确定这是否容易。 或者你可以试试这个:

var hours = Math.abs(date1 - date2) / 36e5;
hh = pad(hours); 

36e5 = 60*60*1000毫秒