loading.js 890 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import {
  2. simpleDebounce,
  3. throttle
  4. } from "./util.js"
  5. import { timeout } from "@/utils/request.js";
  6. let loadingCount = 0; //loading发起时加的计数
  7. let currentState = {};
  8. const defaultState = {
  9. loadingText: "正在加载…",
  10. loadingTime: timeout
  11. }
  12. const endLoading = simpleDebounce(() => {
  13. currentState = {};
  14. uni.hideLoading();
  15. }, 500)
  16. const throttleShowLoading = throttle(500, () => {
  17. uni.showLoading({
  18. title: currentState.loadingText
  19. });
  20. if (currentState.loadingTime) {
  21. setTimeout(() => {
  22. endLoading();
  23. loadingCount = 0
  24. }, currentState.loadingTime)
  25. }
  26. })
  27. export const showLoading = (state = defaultState) => {
  28. if (loadingCount === 0) {
  29. currentState = state;
  30. throttleShowLoading();
  31. }
  32. loadingCount += 1;
  33. };
  34. export const hideLoading = () => {
  35. if (loadingCount <= 0) {
  36. return;
  37. }
  38. loadingCount -= 1;
  39. if (loadingCount === 0) {
  40. endLoading();
  41. }
  42. };