全局loading解决方案(未解决异步请求闪烁问题)

代码实现

思路:记录请求数量,开始请求+1,完成请求回调-1;记录是否正在加载中,如果正加载中不重新开启动画(避免闪烁)

    let loadingCount = 0 // 记录当前正在请求的数量
    let isLoading = false // 是否存着loading
    return new Promise(function (res, rej) {
      if (loadingCount === 0 && !isLoading) {
        wx.showLoading({
          title: '加载中',
        })
        isLoading = true
      }
      loadingCount += 1;
      wx.request({
        url: obj.url ? obj.url : baseApi,
        data: obj.data ? obj.data : {},
        method: obj.method ? obj.method : 'GET',
        header: header,
        success: function (response) {
          res(response.data);
        },
        fail: function (err) {
          rej(err);
        },
        complete: function(){
          if (loadingCount <= 0) {
            return;
          }
          console.log(loadingCount)
          loadingCount -= 1;
          if (loadingCount === 0 && isLoading) {
            wx.hideLoading()
            isLoading = false
          }
        }
      })

    })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值