-
Notifications
You must be signed in to change notification settings - Fork 69
/
UserProgressBar.html
90 lines (76 loc) · 2.41 KB
/
UserProgressBar.html
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Progress Bar</title>
<style>
#container {
height: 30px;
width: 300px;
background-color: lightgrey;
margin-bottom: 5px;
}
#progress {
width: 0%;
height: 100%;
background-color: blue;
}
button {
cursor: pointer;
}
</style>
</head>
<body>
<h1>Progress Bar</h1>
<div id="container">
<div id="progress"></div>
</div>
<button id="btn">Run</button>
<script>
function Progressbar(progressEl, btnEl, seconds) {
let progress = document.querySelector(progressEl);
let runBtn = document.querySelector(btnEl);
let clickCount = 0;
function setClickCount(count) {
runBtn.innerText = Run ${count || ""};
}
function setProgress(n) {
progress.style.width = ${n}%;
}
function doneProgress() {
clickCount--;
setClickCount(clickCount);
// recursively setup progress bar if required
if (clickCount > 0) {
setupProgressInterval(doneProgress);
}
}
function setupProgressInterval(done) {
let progressPct = 0;
let interval = setInterval(() => {
progressPct++;
setProgress(progressPct);
if (progressPct === 100) {
// reset progress after completed
setProgress(0);
clearInterval(interval);
done();
}
}, seconds * 10);
}
function handleButtonClick() {
clickCount += 1;
setClickCount(clickCount);
// call setup on first call only
if (clickCount === 1) {
setupProgressInterval(doneProgress);
}
}
runBtn.addEventListener("click", handleButtonClick);
}
new Progressbar("#progress", "#btn", 3);
</script>
</body>
</html>