Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

231
Visualizações
Making a "CLI-like" progress bar in HTML/JS

So I'm working on a command line interface in the web. Currently, there is the start of a progress bar on the left of the CLI, and a few commands that work. I want to add more commands, but I felt that they needed to have a time delay feature to make it feel more authentic.

I want to make a function that, when called, will divide the (ms) used in the scenario by 12 (the number of dashes in the progress bar) and display an update every (ms).

The Progress bar looks something like this: [------------]

https://codepen.io/ZacV/pen/abEYpLz

function statsBar(ms){
  var timeChunk = Math.round(ms/12)
  for (let i = 0; i < timeChunk; i++) {
    document.getElementById("DispStatus").innerHTML = "[" + ("|" * timeChunk) + ("-" * (12 - timeChunk)) + "]"
    sleep(timeChunk);
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Ok. After a bit of trial and error, I found the most efficient way to finish Rocky Sims snippet. Rocky Sims' code created a really nice and efficient progress bar, however the size of the bar was inconsistent. This final product includes a progress bar with a consistent width to match with the width of the div. Thanks for all of your guys' help on this!

function statsBar(ms){
  var timeChunk = Math.round(ms/12);
  for (let i = 0; i <= 12; i++) {
    setTimeout(() => {
      document.getElementById("DispStatus").innerHTML = "[" + ('|'.repeat(i)) + ('-'.repeat(12 - i)) + "]";
    }, i * timeChunk);
  }
}
statsBar(6000);

<div id="DispStatus"></div>
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use setTimeout to schedule updates to the progress bar all at once but have them happen over time.

function statsBar(ms){
  var timeChunk = Math.round(ms/12);
  for (let i = 0; i < 12; i++) {
    setTimeout(() => {
      document.getElementById("DispStatus").innerHTML = "[" + ('-'.repeat(i)) + "]";
    }, i * timeChunk);
  }
}
statsBar(6000);
<div id="DispStatus"></div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda