Files
JayuCloud/WeChatProgram/utils/loading.js
T

52 lines
791 B
JavaScript

let activeLoadingCount = 0
function showLoading(options) {
const config = options || {}
activeLoadingCount += 1
if (activeLoadingCount > 1) {
return
}
wx.showLoading({
title: config.title || '\u52a0\u8f7d\u4e2d',
mask: config.mask !== false
})
}
function hideLoading() {
activeLoadingCount = Math.max(0, activeLoadingCount - 1)
if (activeLoadingCount > 0) {
return
}
wx.hideLoading({
fail() {}
})
}
function resetLoading() {
activeLoadingCount = 0
wx.hideLoading({
fail() {}
})
}
function runWithLoading(task, options) {
showLoading(options)
return Promise.resolve()
.then(() => task())
.finally(() => {
hideLoading()
})
}
module.exports = {
showLoading,
hideLoading,
resetLoading,
runWithLoading
}