Skip to content

Instantly share code, notes, and snippets.

@adamcrampton
Created September 18, 2019 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcrampton/dc22abaef7a5dd01a31199e85160bef6 to your computer and use it in GitHub Desktop.
Save adamcrampton/dc22abaef7a5dd01a31199e85160bef6 to your computer and use it in GitHub Desktop.
Progress bar that increments itself via a loop
<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