|
10 | 10 | <script>
|
11 | 11 | const progressEl = document.querySelector('[data-id="page-loading-progress"]') as HTMLDivElement;
|
12 | 12 | const storageKey = 'tk_plid';
|
| 13 | + const maxDurationWithoutProgressBar = 500; |
13 | 14 |
|
14 |
| - let expectedDuration = parseFloat(localStorage.getItem(storageKey) || '1000'); |
| 15 | + let expectedDuration = parseFloat(localStorage.getItem(storageKey) || `${maxDurationWithoutProgressBar}`); |
15 | 16 |
|
16 | 17 | let intervalId: ReturnType<typeof setInterval> | undefined;
|
17 | 18 | let timeoutId: ReturnType<typeof setTimeout> | undefined;
|
|
23 | 24 | startTime = Date.now();
|
24 | 25 |
|
25 | 26 | progressEl.style.width = '0%';
|
26 |
| - progressEl.style.opacity = '1'; |
| 27 | + progressEl.style.opacity = '0'; |
27 | 28 |
|
28 | 29 | intervalId = setInterval(() => {
|
29 | 30 | const elapsedTime = Date.now() - startTime;
|
30 | 31 |
|
31 |
| - progressEl.style.width = `${Math.min(elapsedTime / expectedDuration, 1) * 100}%`; |
| 32 | + // we're past the 500ms mark and we're about to make the bar visible, extend the expectedDuration a bit |
| 33 | + if (elapsedTime > maxDurationWithoutProgressBar && expectedDuration <= maxDurationWithoutProgressBar) { |
| 34 | + // this a bit arbitrary to make it look "good" |
| 35 | + expectedDuration += 2 * maxDurationWithoutProgressBar; |
| 36 | + } |
32 | 37 |
|
33 |
| - if (elapsedTime > expectedDuration) { |
34 |
| - clearInterval(intervalId); |
| 38 | + // if expected duration is less we don't show anything |
| 39 | + if (expectedDuration < maxDurationWithoutProgressBar) { |
35 | 40 | return;
|
36 | 41 | }
|
| 42 | + |
| 43 | + progressEl.style.opacity = '1'; |
| 44 | + progressEl.style.width = `${Math.min(elapsedTime / expectedDuration, 1) * 100}%`; |
37 | 45 | }, 100);
|
38 | 46 | }
|
39 | 47 |
|
|
0 commit comments