用于小程序页面倒计时的一个JS
WXML:

<text style="color:{{djscolor}}">当前剩余时间:{{nub}}秒</text>

JS:

Page({
  data: {
    nub:60
  },
 onLoad: function (options) {
    var that = this
    that.setData({
      params:JSON.parse(options.params)//将上级页面跳转时附带的字符串使用 json.parse转换回数组形式
    })
    console.log(that.data.params)
    that.setData({
      nub:60
    })
    // that.countDown();
    that.djs();
  },
  SigninPost:function(){//定义签到提交子程序
    
  },
  djs:function(){//定义倒计时子程序
    var that =this;
    var time=that.data.nub;
    if(time===0 || time<0){
      wx.navigateBack({
        delta: 0,
        success(res){
          console.log("超时退出返回主页面")
        }
      })
    }else{
      if(time<=50){
        that.setData({
          djscolor:"red"
        })
      }
      that.setData({
        nub:that.data.nub-1
      })
      setTimeout(that.djs, 1000);
    }
  }
})
Last modification:January 23, 2022
反正也没人会打赏