setInterval
takes a function as the first parameter, you actually call the function, you only need to pass a link.
In accordance with the documentation, any parameters after a delay will be transferred to the function.
setInterval(increaseVal, 10, progressBars[i]);
To clear the interval when it reaches 100, you will save intervalId, the easiest one will probably just write it to the progress bar.
progressBars[i].interval = setInterval(increaseVal, 10, progressBars[i]); // snip function increaseVal(progressBar){ console.log("In increaseVal(): " + progressBar.value); if (progressBar.value < 100) { progressBar.value = progressBar.value + 1; } else { clearInterval(progressBar.interval); } }
You could also store them in a custom array, but then you have to pass the index of the array to the function.
Check out jsfiddle here
source share