倒计时函数(单个和多条)

2018/04/19 12:41
阅读数 23

1、单个的时候

//倒计时
function countTime(endtime) {
   var countDown = ''
   //获取当前时间
   var date = new Date();
   var now = date.getTime();
   //设置截止时间
   var endDate = new Date(endtime);
   endDate = endDate.getFullYear() > 0 ? endDate : new Date(Date.parse(endtime.replace(/-/g, "/")));
   //var endDate = new Date(time);
   var end = endDate.getTime();

   //时间差
   var leftTime = end - now;
   //定义变量 d,h,m,s保存倒计时的时间
   var d, h, m, s;
   if (leftTime >= 0) {
      d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
      d = addZero(d);
      h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
      h = addZero(h);
      m = Math.floor(leftTime / 1000 / 60 % 60);
      m = addZero(m);
      s = Math.floor(leftTime / 1000 % 60);
      s = addZero(s);
      //setTimeout(countTime,1000);
      //d<1?(countDown=h+':'+m+':'+s):(countDown=d+':'+h+':'+m+':'+s);
      countDown = d + ':' + h + ':' + m + ':' + s;
   } else {
     //结束时
      countDown = '00:00:00:00';
  

   }
   return countDown;
};

//补零
function addZero(str) {
   if (str.toString()
      .length < 2) {
      str = '0' + str;
   }
   return str;
};


调用:
getCountDown() {
   var that = this;
   that.countDown = countTime(EndTime);
   var singalTime = setInterval(function () {
      that.countDown = countTime(EndTime)
   }, 1000);
   if(that.countDown=='00:00:00:00'){
      clearInterval(singalTime)
   }
},

 


2、多条列表同时倒计时

var Alarm = function (endtime, countFunc, endFunc) {
   var date = new Date();
   var startime = date.getTime();
   var endDate = new Date(endtime);
   endDate = endDate.getFullYear() > 0 ? endDate : new Date(Date.parse(endtime.replace(/-/g, "/")));
   endtime = endDate.getTime();
   this.time = Math.floor((endtime - startime) / 1000); //时间
   this.countFunc = countFunc; //计时函数
   this.endFunc = endFunc; //结束函数
   this.flag = 't' + Date.parse(new Date());
};
Alarm.prototype.start = function () {
   var self = this;
   self.flag = setInterval(function () {
      if (self.time < 0) {
         clearInterval(self.flag);
         self.endFunc();
            endTimeTimedTask();
            Reload();
      } else {
         var minute, hour, day, second;
         day = Math.floor(self.time / 60 / 60 / 24) < 10 ? '0' + Math.floor(self.time / 60 / 60 / 24) : Math.floor(self.time / 60 / 60 / 24);
         hour = Math.floor(self.time / 60 / 60 % 24) < 10 ? '0' + Math.floor(self.time / 60 / 60 % 24) : Math.floor(self.time / 60 / 60 % 24);
         minute = Math.floor(self.time / 60 % 60) < 10 ? '0' + Math.floor(self.time / 60 % 60) : Math.floor(self.time / 60 % 60);
         second = Math.floor(self.time % 60) < 10 ? '0' + Math.floor(self.time % 60) : Math.floor(self.time % 60);

         //倒计时执行函数
         self.countFunc(second, minute, hour, day);
         self.time--;
      }
   }, 1000);
}

调用:
for (var i = 0; i < list.length; i++) {
   list[i].showTime = countTime(list[i].endTime);
   (function (ele) {
      alarm = new Alarm(ele.endTime, function (second, minute, hour, day) { //计时钟
         var timeStr = day + ':' + hour + ':' + minute + ':' + second;
         ele.showTime = timeStr;
      }, function () { //倒计时结束
         ele.showTime = '00:00:00:00'
      });
      alarm.start();
   })(list[i]);
   that.resultList.push(list[i]);
  
}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部