代码
var loadingTimers = new Map();
wxRequest(method, url, data, callback, errFun) {
// 加载框显示
const requestId = Symbol();
// 设置加载框显示定时器
loadingTimers.set(
requestId,
setTimeout(() => {
wx.showLoading({
title: "加载中...",
mask: true,
});
}, 1500)
);
method = method.toUpperCase();
data.openid = this.to_openid();
if (!data.phone) {
data.phone = this.to_phone();
}
url = url.replace(/(\.com)(\w)/g, '$1/$2');
wx.request({
url: url,
method: method,
data: data,
header: {
"Content-Type":
method == "GET"
? "application/json"
: "application/x-www-form-urlencoded",
Accept: "application/json",
},
dataType: "json",
timeout: 60000,
enableHttp2: true,
success: function (res) {
// 加载框隐藏
// console.log(loadingTimers, loadingTimers.size);
clearTimeout(loadingTimers.get(requestId));
loadingTimers.delete(requestId);
// console.log(loadingTimers, loadingTimers.size);
if (loadingTimers.size == 0) {
wx.hideLoading(); //隐藏加载框
}
// 回调函数
callback(res.data);
},
fail: function (err) {
errFun(err.data);
},
});
},
如视屏所示这个代码后面会弹出加载中提示