微信小程序实现给一个时间去设置倒计时

设置倒计时

此方法仅限不超过一个月的倒计时

   <view> 
    <text>{{year>9?year:"0"+year}}</text><text>{{month>9?month:"0"+month}}</text><text>{{day>9?day:"0"+day}}</text><text>{{hour>9?hour:"0"+hour}}</text><text>{{minute>9?minute:"0"+minute}}</text><text>{{second>9?second:"0"+second}}</text></view>
page({
  data: {
    day:0,
    hour:0,
    minute:0,
    second:0,
  },
  // 设置倒计时 计算距离结束还有几天几时几分几秒
  setCountDown(endTime) {
    var s1 = new Date(endTime.replace(/-/g, "/")),
      s2 = new Date(),
      runTime = parseInt((s1.getTime() - s2.getTime()) / 1000);
    var year = Math.floor(runTime / 86400 / 365);
    runTime = runTime % (86400 * 365);
    var month = Math.floor(runTime / 86400 / 30);
    runTime = runTime % (86400 * 30);
    var day = Math.floor(runTime / 86400);
    runTime = runTime % 86400;
    var hour = Math.floor(runTime / 3600);
    runTime = runTime % 3600;
    var minute = Math.floor(runTime / 60);
    runTime = runTime % 60;
    var second = runTime;
    if (day < 0 || hour < 0 || minute < 0 || second < 0) {
      console.log("已经结束")
      return;
    }
    this.setData({
      day,
      hour,
      minute,
      second
    })
    console.log("日" + day + ",时" + hour + ",分" + minute + ",秒" + second)
    let that = this
    let _setItv = setInterval(() => {
      that.countDownStart()
    }, 1000);
    this.setData({
      setItv: _setItv
    })
  },
  // 开始做倒计时 
  countDownStart() {
    if (this.data.day == 0 && this.data.hour == 0 && this.data.minute == 0) {
      if (this.data.second > 0) {
        this.setData({
          second: this.data.second - 1
        })
      } else if (this.data.second == 0) {
        clearInterval(this.data.setItv)
        console.log("倒计时结束")
      }
    } else if (this.data.day == 0 && this.data.hour == 0) {
      // 不会出现minute等于0
      if (this.data.minute > 0) {
        if (this.data.second > 0) {
          this.setData({
            second: this.data.second - 1
          })
        } else if (this.data.second == 0) {
          this.setData({
            second: 59,
            minute: this.data.minute - 1
          })
        }
      }
    } else if (this.data.day == 0) {
      if (this.data.hour > 0) {
        if (this.data.minute > 0) {
          if (this.data.second > 0) {
            this.setData({
              second: this.data.second - 1
            })
          } else {
            this.setData({
              second: 59,
              minute: this.data.minute - 1
            })
          }
        } else {
          this.setData({
            second: 59,
            minute: 59,
            hour: this.data.hour - 1
          })
        }
      }
    } else {
      if (this.data.day > 0) {
        if (this.data.hour > 0) {
          if (this.data.minute > 0) {
            if (this.data.second > 0) {
              this.setData({
                second: this.data.second - 1
              })
            } else {
              this.setData({
                second: 59,
                minute: this.data.minute - 1
              })
            }
          } else {
            this.setData({
              second: 59,
              minute: 59,
              hour: this.data.hour - 1
            })
          }
        } else {
          this.setData({
            second: 59,
            minute: 59,
            hour: 23,
            day: this.data.day - 1
          })
        }
      }
    }
  },
})
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值