Created
September 18, 2019 03:58
-
-
Save adamcrampton/dc22abaef7a5dd01a31199e85160bef6 to your computer and use it in GitHub Desktop.
Progress bar that increments itself via a loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="progress-bar"><div style="width:0;"></div> | |
<script> | |
let progressBar = document.querySelector("#progress-bar div") | |
let items = []; | |
for (let index = 0; index <= 100; index++) { | |
items.push(index); | |
} | |
delayedLoop(items, 10, function(item, index) { | |
let width = (item / items.length * 100) + "%"; | |
progressBar.style.width = width; | |
}); | |
function delayedLoop(collection, delay, callback, context) { | |
context = context || null; | |
let i = 0, | |
nextInteration = function() { | |
if (i === collection.length) { | |
return; | |
} | |
callback.call(context, collection[i], i); | |
i++; | |
setTimeout(nextInteration, delay); | |
}; | |
nextInteration(); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment