js 倒计时编写

 countDownCtrl() {
    
    
    let that = this
    let startTime = new Date().getTime()
    let endTime = new Date(that.data.endTime_ * 1000).getTime()
    let time = (endTime - startTime) /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);
    if (time>0){
    
    
      setTimeout(this.countDownCtrl, 1000);
      that.setData({
    
    
        timer : day + "天 " + that.timeFormat(hou) + ":" + that.timeFormat(min) + ":" + that.timeFormat(sec)
      })
    }
  },
   //小于10的格式化函数(2变成02)
   timeFormat(param) {
    
    
    return param < 10 ? '0' + param : param;
  },

注释:

  1. startTime 为当前时间,
  2. endTime 为结束时间,如果你转化后的时间为1970年,请乘以1000

猜你喜欢

转载自blog.csdn.net/jiaodeqiangs/article/details/103961058
今日推荐