/*
1. Make a progress bar that runs from 0-100% on click of a button
2. Increment progress at random between 1-10 every random time (1-100 ms)
3. Alert seconds it took once it reaches 100%
4. Add debounce function so it only fires every 100 ms
5. Make 10 more loaders that all run when you click 'load'
6. Make a master loader that shows the average progress of all 10 loaders
*/
const bar = document.querySelectorAll('.bar');
const button = document.querySelector('button');
console.log(bar)
button.addEventListener('click', () => {
onClick();
});
let currentProgress = 0;
let startTime;
const debounce = (callback, time = 100) => {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => callback(...arg), time);
}
}
const displayProgress = (progress) => {
bar.style.width = `${progress}%`;
bar.innerHTML = `${progress}%`;
}
const onClick = () => {
startTime = new Date().getTime();
onLoaderProgress();
}
const onLoaderProgress = () => {
let randomNumber = Math.random() * 9 + 1;
let newProgress = Math.min(currentProgress + randomNumber, 100);
currentProgress = newProgress;
debounce(displayProgress(newProgress), 100);
let randomInterval = Math.floor(Math.random() *100 +1);
if (currentProgress < 100) {
setTimeout(onLoaderProgress, randomInterval);
}
if (currentProgress === 100) {
onComplete();
}
}
const onComplete = () => {
console.log("Loader completed in: " + ((new Date().getTime() - startTime) / 1000) + " seconds");
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.