团购倒计时js

//获取今天日期
function GetDateStr(AddDayCount)
{
  var dd = new Date();
  dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
  var y = dd.getFullYear();
  var m = dd.getMonth()+1;//获取当前月份的日期
  var d = dd.getDate();
  return y+"-"+m+"-"+d;
}
 /*
    倒计时应用(闭包) V1.0
    需要引用Jquery版本1.2以上
    Html:<div id="必填" class="time" ></div>
    ID:必填项,
    class:为time,可以修改,将对应的修改$(".time")中的值
    endtime:为结束时间 时间格式{yyyy-MM-dd hh:mm:ss}
    */
    (function () {
         
        var  D = "<li><i>{1}</i></li><li><i>{2}</i></li><li><i>{3}</i></li>"; //可以使用其他的显示字符串或Html
        var  DD = "剩余时间:<span>{1}</span>时<span>{2}</span> 分<span>{3}.{4}</span>秒"; //可以使用其他的显示字符串或Html
        function H(k, j) {return k.replace(/{(\d)}/g, function (l, m) {return j[m]})}
        function B(j) {return j < 10 ? "0" + j : j}
        var F = function (s) {
            if (s > 0) {
                /* 1分=60秒 1天=1440分 1天=86400秒 */
                var _s = s % 60, _m = (s - _s) / 60, _h = parseInt(_m / 60), _d = parseInt(_h / 24)
                _h = _h % 24;
                _m = _m % 60;
                return H(D, [_d, B(_h), B(_m), B(_s)]);
              
            }
       
            return s;
        }
        
         var Fms = function (s,ms) {
            if (s > 0) {
                /* 1分=60秒 1天=1440分 1天=86400秒 */
                var _s = s % 60, _m = (s - _s) / 60, _h = parseInt(_m / 60), _d = parseInt(_h / 24),_ms=ms;
                _h = _h % 24;
                _m = _m % 60;
                return H(DD, [_d, B(_h), B(_m), B(_s),_ms]);
            }
       
            return s;
        }
        function dj_time(id, endtime,flag) {
            this.ID = id;
            this.EndTime = endtime;
            this.Flag=flag;
            this.ServerTime = null;
            this.Diff = null;
            this.ms = null;//计算0.1秒
            this.IsAjax = false; //是否开启ajax
            this.onClock = function () {
                if (this.IsAjax) { /* 可以使用ajax获取远程的时间 */return;}
                if (this.ServerTime == null || isNaN(this.ServerTime)) {
                    // 获取服务器时间可以通过Ajax
                    this.IsAjax = true;
                    this.ServerTime = new Date().getTime();
                    this.IsAjax = false;
                }
                if (isNaN(this.Diff) || this.Diff == undefined || this.Diff == null) {
                    var end = new Date(this.EndTime.replace(/-/g, "/")).getTime(); /* 适用于2010-08-23  */
                    if (isNaN(end)) {end = new Date(this.EndTime.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1/$2/$3")).getTime();} /* 适用于20100823  */
                    this.Diff = parseInt((end - this.ServerTime) / 1000);
                     
                     this.ms=this.Diff%10;
                }
              
                if((isNaN(this.Diff)||this.Diff<=0))
                {
                   if(this.Flag==0)
                     {
                    location.href=location.href;
                    }
                    
                   $("#limitbuy").removeClass("green_btn").addClass("gray_button").removeAttr("href").text("已售完");
                   return 0;
                }
            
              if(this.Flag==0)
              {
                this.Diff -= 1;
                 this.Diff==0 ?document.getElementById(this.ID).innerHTML ="<li><i>00</i></li><li><i>00</i></li><li><i>00</i></li>":document.getElementById(this.ID).innerHTML = F(this.Diff);
              }
              else
              {
                 this.ms+=1
              if(this.ms==10)
             {
                 this.ms=0;     
                 this.Diff -= 1;
             }
               this.Diff==0 ?document.getElementById(this.ID).innerHTML ="剩余时间:<span>00</span>时<span>00</span>分<span& gt;0.0</span>秒":document.getElementById(this.ID).innerHTML = Fms(this.Diff,this.ms);     
               }
              
            }
            return this;
            
        }
        var arr = [], arr2 = [],endtime=GetDateStr(0)+" 11:00:00",limitendtime=GetDateStr(0)+" 10:00:00";
        /* 循环获取需要进行倒计时的dom,这里使用了jquery,习惯而已,可以使用其他的框架 */
        $(".time").each(function (i) {arr[i] = new dj_time($(this).attr("id"),endtime,0)});
         $(".sale_time").each(function (i) {arr2[i] = new dj_time($(this).attr("id"),limitendtime,1)});
       var K = function () {for (var i in arr) arr[i].onClock();}, K2 = function () {for (var i in arr2) arr2[i].onClock();}, T = setInterval(function () {K()}, 1000), T2 = setInterval(function () {K2()}, 100);
    })();

posted @ 2013-07-18 12:15  梦似叶落  阅读(251)  评论(0)    收藏  举报