export default class loadingPanel {
private loadingCount = 0;
private baseDelay: number; // 延时时间,该时间内的请求将不会显示loading效果
private allowLoading = true;
constructor(delay = 0) {
this.baseDelay = delay;
}
addLoadingPanel = (loadingStartFn: () => void) => {
if (this.allowLoading) {
this.loadingCount++;
if (this.loadingCount > 0) {
loadingStartFn();
}
}
};
decLoadingPanel = (loadingEndFn: () => void, immediately = false) => {
if (this.baseDelay > 0) {
this.allowLoading = false;
setTimeout(() => {
this.allowLoading = true;
}, this.baseDelay);
}
this.loadingCount--;
if (immediately) {
this.loadingCount = 0;
}
if (this.loadingCount < 0) {
this.loadingCount = 0;
}
if (this.loadingCount === 0) {
loadingEndFn();
}
};
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39