wx.showToast一闪而过微信提示一闪而过

调试的时候发现toast闪过的太快了,而且在模拟器上是没有问题的,在真机上就不行

后来给toast增加时长也不管用,了解了之后总结一下问题

toast闪过的快有两种情况

  • 一种是加载的时候先是showloading,加载完成之后弹toast提示一下成功或者失败,这个toast关闭的特别快,后来发现showloading和hideloading必须得是搭配使用的,就是说如果showloading之后紧跟着showtoast的话是不行的,所以在showtoast之前一定要先关闭loading
//打开loading
wx.showLoading({
    
    
    title: '加载中...',
    mask: true,
});
wx.request({
    
    
    url:url,
    data: {
    
    
       ...
    },
    header: {
    
    
        "Content-Type": "application/x-www-form-urlencoded"
    },
    method: "POST",
    dataType: "json",
    success(res) {
    
    
    //关闭loading
        wx.hideLoading()
    //打开toast
        wx.showToast({
    
    
            title: res.data.msg,
            icon: 'none',
            duration: 3000
        });
    },
    fail() {
    
    
    //关闭loading
        wx.hideLoading()
    //打开toast
        wx.showToast({
    
    
            title: '发送失败',
            icon: 'none',
            duration: 3000
        });
    }
})

要先关闭loading再打开toast,就不会一闪而过了

  • 还有一种情况就是,提示成功之后跳转页面,在A页面提示之后跳转到B页面,toast不会跟随着展示到B页面,toast在跳转的时候就关闭了,所以把跳转放在toast执行之后再执行
wx.showToast({
    
    
    title: '绑定成功',
    icon: 'success',
    duration: 3000,
    success() {
    
    
        //计时器
        setTimeout(function () {
    
    
            wx.switchTab({
    
    
                url: '/pages/mine/mine',
                fail: (e) => {
    
    
                    console.log(e)
                    wx.showToast({
    
    
                        title: e.errMsg || err.msg || e,
                        icon: 'none',
                        duration: 3000
                    });
                }
            });
        }, 3000)
    }
});

加一个计时器,等toast执行完了再跳转,toast就能正常展示了

猜你喜欢

转载自blog.csdn.net/glorious_future/article/details/115392988