JavaScript 倒计时(天 时分秒)

 timeDiff 为时间差  (单位:毫秒)

activityStartCount(timeDiff: number) {
    if (templateStarInterval) {
      clearInterval(templateStarInterval)
      templateStarInterval = null
    }
    templateStarInterval = setInterval(() => {
      timeDiff -= 1000
      if (timeDiff < 0 ) {
        clearInterval(templateStarInterval)
        templateStarInterval = null
       
        return
      }
      // let timeDiff = (starCNum - Time)
      // 将时间差转换为天、小时、分钟和秒
      let days = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
      let hours = Math.floor((timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      let minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60));
      let seconds = Math.floor((timeDiff % (1000 * 60)) / 1000);
      // 在页面上展示倒计时
      this.setData({ templateStarTime: `${days > 0 ? (days + '天'): ''}${hours > 9 ? hours : '0' + hours}:${minutes > 9 ? minutes : '0' + minutes}:${seconds > 9 ? seconds : '0' + seconds}` })
    }, 1000)
  },

你可能感兴趣的:(java,前端,javascript)