I don’t know how implement and call my functions in a way that my progress bar be completed when I click in the button that I define to execute my function
I just want to know how I can make my code just run over and over until a condition be satisfied.
const linhasProgresso = document.querySelectorAll('.linha')
linhasProgresso.forEach((linhaProgresso, index) => {
if (index < controle) {
setInterval(progressoPositivo(linhaProgresso), 10)
} else {
setInterval(progressoNegativo(linhaProgresso), 10)
}
})
function progressoPositivo(elem) {
if (width < 100) {
width++
elem.style.width = width + '%';
}
}
function progressoNegativo(elem) {
if (width > 0) {
width--
elem.style.width = width + '%';
}
}
I think that my problem occurs when I call my functions. I want it to be so that my progress bar be with width = 100%
when I click on my button just one time.
3
Answers
The HTML progress bar should have a set width and its percentage should be expressed in the value attribute. Rather than change the size of the progress bar (an element which contains a graphical representation of the percentage), you should merely change the percentage the progress bar shows using the value attribute.
Instead of changing progressbar.style.width, you should set the width at the outset and never change it (outside of a resize event) and use the progressbar.value to express how much actual progress has been carried out.
You need to capture the return value of
setInterval
so that you can callclearInterval
to stop the process (see here). The best way is to pass an anonymous function that checks if the process is complete. Yourprogresso*
functions should return a value indicating whether or not, the process is complete.If you are just toggling one step at a time, consider using a css transition for a much cleaner solution that runs smoother.