微信小程序中wx.showToast延时执行

作者: webhome 发布时间: 2019-11-08 浏览: 3285 次 编辑

微信小程序中wx.showToast延时执行

在做微信小程序项目开发时需要对用户交互的数据状态结果进行反馈,通过会用到dialog和toast等方法来实现,在反馈之后还有可能需要执行跳转等功能操作。拿Toast来讲,用户提交订单,订单提交成功,用toast来进行反馈,toast展示完成后进行跳转,进入会员中心页面,或者订单列表页。默认的情况是toast刚显示就进行了跳转操作,因此,需要对跳转操作进行延时处理。需要用到代码:

setTimeout(function () {  
     //要延时执行的代码  
}, 1000) //延迟时间 这里是1秒

项目代码示例:

//提交预约订单  wx.request({
    url: 'http://www.pusonglin.cn/app/index.php?i=2&c=entry&do=api&op=addOrder&m=aiunv_book',
    data: e.target.dataset,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      console.log(res.data)
 
      let status = res.data.status;
      if (status == 1) {
        console.log('预定成功')
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 2000,
          success:function(){
            console.log('haha');
            setTimeout(function () {
              //要延时执行的代码
              wx.switchTab({
                url: '../user/user'
              })
            }, 2000) //延迟时间 
          }
        })
 
 
      }
      if (status == 0) {
        console.log('失败');
      }
 
      that.setData({ addrArray: res.data });
    }
  });

另外,在内页做页面跳转需要用wx.switchTab。