Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
How to create a dynamic progress bar forwards and backwards with the button controls stop and resume?

I am trying to create a dynamic progress bar forwards and backward with the button controls to stop and resume, how to add the logic to the function? Thanks!

//PROGRESS
function updateBar() {
  if (i == 0) {
    i = 1;
    let filling = document.querySelector(".progress-filling");
    let width = 0;
    setInterval(update, 5);
    function update() {
      if (width <= 100) {
        width++;
        filling.style.width = width + "%";
      } else {
        width--;
        filling.style.width = width + "%";
      }
    }
  }
}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need to check status of your button (i) inside the update loop and do nothing if it's stopped:

//PROGRESS
let btn = document.querySelector(".btn");
btn.textContent = "STOP";
btn.addEventListener("click", changeInnerHTML);
function changeInnerHTML() {
  btn.textContent = btn.textContent === "STOP" ? "RESUME" : "STOP";
  if (btn.textContent === "RESUME") {
    i = 0;
  } else if (btn.textContent === "STOP") {
    i = 1;
  }
}

let i = 1;
updateBar();

function updateBar() {
    let filling = document.querySelector(".progress-filling");
    let width = 0;
    let step = 1;
    setInterval(update, 5);
    function update() {
      if (!i)
        return;

      width += step;
      filling.style.width = width + "%";
      if (width >= 100 || width <= 0)
        step = -step; //reverse

    }
}
.progress-filling
{
  height: 2em;
  border: 1px solid black;
  background-color: lightgreen;
}
<button class="btn">STOP</button>
<div class="progress-filling"></div>

about 4 years ago · Juan Pablo Isaza Report

0

I used progress element here u can use your wdth etc.... But Checkout the logic I think it's what you are looking for !

<progress id="progress" min="0" max="100" value="0"></progress>
<button id="progress-ctrl">START</button>
<button id="progress-stop">STOP</button>

Js


  const progress = document.querySelector('#progress');
  const button = document.querySelector('#progress-ctrl');
  const sbutton = document.querySelector('#progress-stop');
  let interval;
  const time = 50; //in ms
  button.addEventListener('click', (e)=> {
    const text = button.innerText.toLowerCase();
    switch (text) {
      case 'start':
        start();
        button.innerText = "PAUSE";
        break;

      case 'pause':
        pause()
        button.innerText = "RESUME";
        break;

      case 'resume':
        resume()
        button.innerText = "PAUSE";
        break;

      default:
        // nothing to do
      }
    });
    sbutton.addEventListener('click', (e)=> {
      sbutton.innerText = "STOPPED";
      button.innerText = "START";
      stop();
    });

    function start() {
      interval = setInterval(function() {
        progress.value += 1
      }, time);
      sbutton.innerText = "STOP";
    }
    function pause() {
      clearInterval(interval);
    }
    function resume() {
      interval = setInterval(function () {
        progress.value += 1
      }, time);
    }
    function stop() {
      clearInterval(interval);
      progress.value = 0;
    }
 
```
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!