小程序秒杀商品列表倒计时(vue可以复用)

实现效果
小程序秒杀商品列表倒计时(vue可以复用)_第1张图片
列表秒杀倒计时

//例:返回数据数组
let goodsList = [{
  goodsEndTime: '2019-04-09 18:00:43'
}]
let endTimeList = [];
//定义一个新数组,将活动的结束时间参数提成一个单独的数组
for(let i=0;i
//小于10的格式化函数
timeFormat(times) { 
    return times< 10 ? '0' + times: times;
  },
//倒计时函数
 countDown() { 
    // 获取当前时间,同时得到活动结束时间数组
    let newTime = new Date().getTime();
    let endTimeList = this.data.actEndTimeList;
    let countDownArr = [];

    // 对结束时间进行处理渲染到页面
    endTimeList.forEach(o => {

      let endTime = new Date(o.eTime ).getTime();
      let obj = null;
      // 如果活动未结束,对时间进行处理
      if (endTime - newTime > 0) {
        let time = (endTime - newTime) / 1000;
        // 获取天、时、分、秒
        let day = parseInt(time / (60 * 60 * 24));
        let hou = parseInt(time % (60 * 60 * 24) / 3600);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: this.timeFormat(day),
          hou: this.timeFormat(hou),
          min: this.timeFormat(min),
          sec: this.timeFormat(sec)
        }
      } else { //活动已结束,全部设置为'00'
        obj = {
          day: '00',
          hou: '00',
          min: '00',
          sec: '00'
        }
      }
      countDownArr.push(obj);
    })
    // 渲染,然后每隔一秒执行一次倒计时函数
    this.setData({
      countDownList: countDownArr
    })
    setTimeout(this.countDown, 1000);
  },
//WXML
		
			 剩余时间
	        {{item.overTime}}
	        
	        
		          {{countDownList[index].day}}{{countDownList[index].hou}}{{countDownList[index].min}}{{countDownList[index].sec}}
       

你可能感兴趣的:(vue,微信小程序,js)