关于wx.showLoading有多个请求的问题
发布于 5 年前 作者 kmeng 11176 次浏览 来自 问答

公司这个项目有的页面请求接口多的时候 会请求3-6个 但是使用wx.showLoading的时候 只要第一个接口请求完毕 就会执行wx.hideLoading 这样有一个接口先执行完就会导致loading没有了 请问可以给wx.showLoading指定变量,wx.hideLoading关闭指定的loading吗 或者有其他的骚操作 因为业务逻辑问题 接口也不能合并 只能单个请求

2 回复

test(e)  {

var that = this

that.setData({

loadData: true

})

wx.showLoading({

title: ‘数据下载中’,

})

var url1 = that.data.editData.shop_swiperpic

var url2 = that.data.editData.shop_detailpic

var url3 = that.data.editData.shop_logopic

var a = function () {

return new Promise(function (resolve, reject) {

if (!url1.length) {

resolve(“a”)

} else {

util.downloadSaveFiles({

urls: url1,

success: function (res) {

resolve(“a”)

},

fail: function (e) {

console.info(“下载失败”);

}

}, that, ‘swiperpic’, ‘正在下载轮播图片’);

}

})

}

var b = function (data) {

return new Promise(function (resolve, reject) {

if (!url2.length) {

resolve(data + ‘b’)

} else {

util.downloadSaveFiles({

urls: url2,

success: function (res) {

resolve(data + ‘b’)

},

fail: function (e) {

console.info(“下载失败”);

}

}, that, ‘detailpic’, ‘正在下载详情图片’);

}

})

}

var c = function (data) {

return new Promise(function (resolve, reject) {

if (!url3.length) {

resolve(data + ‘c’)

} else {

wx.showLoading({

title: ‘即将完成下载’,

})

util.downloadSaveFiles({

urls: url3,

success: function (res) {

resolve(data + ‘c’)

},

fail: function (e) {

console.info(“下载失败”);

}

}, that, ‘logopic’, ‘正在下载商家LOGO’);

}

})

}

function reduce(arr) {

var sequence = Promise.resolve()

arr.forEach(function (item) {

sequence = sequence.then(item)

})

return sequence

}

reduce([a, b, c])

.then(function (data) {

setTimeout(function(){

wx.showLoading({

title: ‘数据下载完成’,

})

that.edit()

},1000)

})

.catch(function (e) {

console.log(e)

})

},

我们的逻辑大概就是这样子,

用个蠢方法设置一个number=0;每个接口调用成功后number+1;然后在成功里面判断这个number==你调用的接口数,那就表示你的接口全部调用成功了。。。或者用promise

回到顶部