小程序定时刷新问题

博客主要讲述小程序定时刷新问题。使用mqtt通信时,定时刷新不能放在onShow()里,否则会造成页面死循环。调用时在onLoad里执行this.intervalRefresh(),清除interval在mqtt接收消息中用clearInterval()。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

小程序定时刷新问题
坑: 在使用mqtt通信时,定时刷新不能放在onShow()里面 ,否则会导致页面死循环(在各个页面不停的跳转)

 intervalRefresh: function (){
    var _this = this
    var timer = setInterval(function () {
      app.util.reqAsync('接口', {
        presaleId: _this.data.orderId,
        businessId: _this.data.businessId
      }).then((res) => {
        if (res.data.code == 1) {
          // console.log(res,'-----res1')
          if (res.data.data != []) {
            _this.setData({
              orderStatus: res.data.data[0].orderStatus,
              payStatus: res.data.data[0].payStatus
            })
            if (res.data.data[0].orderStatus == 4) {
              _this.sendMsg()
            }
          }
        }
      })
    }, 10000)
    _this.setData({
      timer
    })
  },

调用:
onLoad: function (options) {
this.intervalRefresh()

}

清除interval 则在mqtt 的接收消息中 clearInterval()

以上。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值