位置:海鸟网 > IT > JavaScript >

JavaScript实现倒计时的代码

/* 倒计时的实现
* 参数:d:
* 参数:o:
实例:
var zxx = {
    $: function(id){
        return document.getElementById(id);   
    },
    futureDate: Date.UTC(2010, 10, 2, 11,17,49),
    obj: function(){
        return {
            sec: zxx.$("sec"),////需要建立相应的id的元素
            mini: zxx.$("mini"),
            hour: zxx.$("hour"),
            day: zxx.$("day"),
            month: zxx.$("month"),
            year: zxx.$("year"),
            q:zxx.$("jsq"),
   messid:zxx.$("mess"),
   mess:"时间结束了"
        }
    }
};
fnTimeCountDown(zxx.futureDate, zxx.obj());

 <span id="year"></span> <span id="month"></span> <span id="day"></span> <span id="hour"></span> <span id="mini"></span>  <span id="sec"></span>
<div id="mess">  </div>
*/
var fnTimeCountDown = function(d, o,stime){
 var f = {
  zero: function(n){
   var n = parseInt(n, 10);
   if(n > 0){
    //if(n <= 9){
    // n = "0" + n;
    //}
    return String(n);
   }else{
    return "00";
   }
  },
  dv: function(){
   d = d || Date.UTC(2050, 0, 1); //如果未定义时间,则我们设定倒计时日期是2050年1月1日
   var future = new Date(d), now = new Date();
   //现在将来秒差值
    var dur = Math.round((future.getTime() - now.getTime()) / 1000)  , pms = {
    sec: "00",
    mini: "00",
    hour: "00",
    day: "00",
    month: "00",
    year: "0"
   },
   mes="1"
   ;
   if(dur >= 0){
    pms.mes="1";
    pms.sec = f.zero(dur % 60);
    pms.mini = Math.floor((dur / 60)) > 0? f.zero(Math.floor((dur / 60)) % 60) : "00";
    pms.hour = Math.floor((dur / 3600)) > 0? f.zero(Math.floor((dur / 3600)) % 24) : "00";
    pms.day = Math.floor((dur/3600/24)) > 0? f.zero(Math.floor((dur/3600/24))) : "00";
    //月份,以实际平均每月秒数计算
   // pms.month = Math.floor((dur / 2629744)) > 0? f.zero(Math.floor((dur / 2629744)) % 12-1) : "00";
    //年份,按按回归年365天5时48分46秒算
   // pms.year = Math.floor((dur / 31556926)) > 0? Math.floor((dur / 31556926)) : "0";
   } else
   pms.mes="0";
   return pms;
  },
  ui: function(){
    if(f.dv().mes=="0"){
     if(o.jsq)o.jsq.style.display="none";
     if(o.messid&&o.mess)o.messid.innerHTML=o.mess;
    }
   if(f.dv().mes=="1"){
    if(o.sec){
     o.sec.innerHTML = f.dv().sec+"\u79D2";
   }
   if(o.mini){
     o.mini.innerHTML = f.dv().mini+"\u5206";
   }
   if(o.hour){
     o.hour.innerHTML = f.dv().hour+"\u5C0F\u65F6";
   }
   if(o.day){
    if(f.dv().day!="00")
    o.day.innerHTML = f.dv().day+"\u5929";
   }
   if(o.month){
    if(f.dv().month!="00")
    o.month.innerHTML = f.dv().month+"\u6708";
   }
   if(o.year){
    if(f.dv().year!="0")
    o.year.innerHTML = f.dv().year+"\u5E74";
   }
   if(stime!=0){
   setTimeout(f.ui, stime);
   }
   }
  }
 };
 f.ui();
};
function djs(year, month, day, hour,min,sec,stime){
  var zxx = {
      _$: function(id){
          return document.getElementById(id);   
      },
      futureDate: Date.UTC(year, month-1, day, hour,min,sec),
      obj: function(){
          return {
              sec: zxx._$("sec"),////需要建立相应的id的元素
              mini: zxx._$("mini"),
              hour: zxx._$("hour"),
              day: zxx._$("day"),
              month: zxx._$("month"),
              year: zxx._$("year"),
              jsq:zxx._$("jsq"),
     messid:zxx._$("mess"),
     mess:"已经结束了"
          }
      }
  };
  fnTimeCountDown(zxx.futureDate, zxx.obj(),1000);
}
function viewT(id,year, month, day, hour,min,sec,stime){
 var zxx = {
      _$: function(id){
          return document.getElementById(id);   
      },
      futureDate: Date.UTC(year, month-1, day, hour,min,sec),
      obj: function(){
          return {
              hour: zxx._$("hour"+id),
              day: zxx._$("day"+id),
              month: zxx._$("month"+id),
              year: zxx._$("year"+id)
         }
      }
  };
  fnTimeCountDown(zxx.futureDate, zxx.obj(),stime);
  }