| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import {
- simpleDebounce,
- throttle
- } from "./util.js"
- // 与 request.js 中 timeout 保持一致,避免循环依赖(request.js 会 import 本文件的 showLoading/hideLoading)
- const DEFAULT_LOADING_TIME = 1000 * 60;
- let loadingCount = 0; //loading发起时加的计数
- let currentState = {};
- const defaultState = {
- loadingText: "正在加载…",
- loadingTime: DEFAULT_LOADING_TIME
- }
- const endLoading = simpleDebounce(() => {
- currentState = {};
- uni.hideLoading();
- }, 500)
- const throttleShowLoading = throttle(500, () => {
- uni.showLoading({
- title: currentState.loadingText
- });
- if (currentState.loadingTime) {
- setTimeout(() => {
- endLoading();
- loadingCount = 0
- }, currentState.loadingTime)
- }
- })
- export const showLoading = (state = defaultState) => {
- if (loadingCount === 0) {
- currentState = state;
- throttleShowLoading();
- }
- loadingCount += 1;
- };
- export const hideLoading = () => {
- if (loadingCount <= 0) {
- return;
- }
- loadingCount -= 1;
- if (loadingCount === 0) {
- endLoading();
- }
- };
|