js countdown writing

 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;
  },

Notes:

  1. startTime is the current time,
  2. endTime is the end time, if your converted time is 1970, please multiply by 1000

Guess you like

Origin blog.csdn.net/jiaodeqiangs/article/details/103961058