背景:开发微信小程序,协同点单时,需要一直向后端请求数据,所以用到了setInterval,如果从点单页面跳转到其他页面,再回来就会发现两次onshow之后 setInterval,执行的次数多了,所以需要用clearInterval
onShow: function () { var that = this that.getOrder() }, getOrder:function(){ var that = this if (that.data.interval) { clearInterval(that.data.interval) } var interval = setInterval(function () { // ... 获取订单 }, 2000) that.setData({ interval: interval }) },