-
Notifications
You must be signed in to change notification settings - Fork 1
/
UberProgressBar.js
56 lines (48 loc) · 1.5 KB
/
UberProgressBar.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const container = document.getElementById("container");
const startButton = document.getElementById("start_btn");
const maxConcurrency = 3;
let runningBars = 0;
const progressQueue = [];
const bars = [
{ duration: 3000, color: "red" },
{ duration: 3000, color: "blue" },
{ duration: 3000, color: "red" },
{ duration: 3000, color: "pink" },
{ duration: 3000, color: "red" },
];
startButton.addEventListener("click", () => {
if (bars.length > 0) {
const { duration, color } = bars.shift();
createLoadingBar(duration, color);
}
});
function createLoadingBar(duration, color) {
if (runningBars < maxConcurrency) {
runningBars++;
generateBar(duration, color);
} else {
progressQueue.push({ duration, color });
}
}
function generateBar(duration, color) {
const loadingBar = document.createElement("div");
loadingBar.classList.add("loading_bar");
loadingBar.style.backgroundColor = color;
container.appendChild(loadingBar);
const startTime = Date.now();
function updateProgress() {
const elapsedTime = Date.now() - startTime;
const progressPercentage = Math.min((elapsedTime / duration) * 100, 100);
loadingBar.style.width = progressPercentage + "%";
if (progressPercentage < 100) {
requestAnimationFrame(updateProgress);
} else {
runningBars--;
if (progressQueue.length > 0) {
const nextBar = progressQueue.shift();
createLoadingBar(nextBar.duration, nextBar.color);
}
}
}
requestAnimationFrame(updateProgress);
}