微信小程序倒计时实现功能

时间:2022-02-08 22:03:41

 onLoad: function () {
    var that=this;
    this.data.intervarID= setInterval(function () {
      var leftTime = (new Date(2017, 6-1, 28, 17, 40, 0)) - (new Date()); //计算剩余的毫秒数
      var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
      var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时
      var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟
      var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数
      days = checkTime(days);
      hours = checkTime(hours);
      minutes = checkTime(minutes);
      seconds = checkTime(seconds);
      that.setData({
        clock: days + "天" + hours + "小时" + minutes + "分" + seconds + "秒"
      })
      if (days == '00' && hours == '00' && minutes == '00' && seconds=='00')
      {
        clearInterval(that.data.intervarID);
      }
    }, 1000
    )
  },