Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
How to add timer inside the progress bar

I want to add a timer which decrease Automatically (like 10 seconds, 9 seconds... till 0 seconds) but the progress bar will increase. And I am new to javascript, and the below code also copied from another site , so please help me in adding timer inside the progress bar

Till now I did this code

I want to make like this Demo

<div class="progress"></div>

<style>
.progress-bar {
  height: 20px;
  background: #1da1f2;
  box-shadow: 2px 14px 15px -7px rgba(30, 166, 250, 0.36);
  border-radius: 50px;
  transition: all 0.5s;
}
.progress {
  width: 100%;
  display: flex;
  flex-flow: column nowrap;
  justify-content: center;
  align-items: start;
  background: #e6e9ff;
  border-radius: 20px;
  box-shadow: 0px 10px 50px #abb7e9;
}
  </style>
<script>
  /* 
 * (class)Progress<nowValue, minValue, maxValue>
 */

//helper function-> return <DOMelement>
function elt(type, prop, ...childrens) {
  let elem = document.createElement(type);
  if (prop) Object.assign(elem, prop);
  for (let child of childrens) {
    if (typeof child == "string") elem.appendChild(document.createTextNode(child));
    else elem.appendChild(elem);
  }
  return elem;
}

//Progress class
class Progress {
  constructor(now, min, max, options) {
    this.dom = elt("div", {
      className: "progress-bar"
    });
  
    this.min = min;
    this.max = max;
    this.intervalCode = 0;
    this.now = now;
    this.syncState();
    if(options.parent){
      document.querySelector(options.parent).appendChild(this.dom);
    } 
    else document.body.appendChild(this.dom)
  }

  syncState() {
    this.dom.style.width = this.now + "%";
  }
startTo(step, time) {
    if (this.intervalCode !== 0) return;
    this.intervalCode = setInterval(() => {
      console.log("sss")
      if (this.now + step > this.max) {
        this.now = this.max;
        this.syncState();
        clearInterval(this.interval);
        this.intervalCode = 0;
        return;
      }
      this.now += step;
      this.syncState()
    }, time)
  }
  end() {
    this.now = this.max;
    clearInterval(this.intervalCode);
    this.intervalCode = 0;
    this.syncState();
  }
}

  let pb = new Progress(15, 0, 100, {parent : ".progress"});

//arg1 -> step length
//arg2 -> time(ms)
pb.startTo(5, 500);

//end to progress after 5s
setTimeout( () => {
  pb.end()
}, 10000)
  </script>
almost 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I think the core problem is that the code you copied is overly complicated especially for beginners. What I would recommend is to start from what you know and build up.

Here is the functionality you want written using only core principles of JavaScript and CSS.

let initialTime = 10; //All time in seconds
let timeLeft = initialTime;

let interval;
let progressBarTextElement = document.getElementById('progress-bar-text');
let progressBarElement = document.getElementById('progress-bar');

function render() {
  let progressPercentage = (1 - (timeLeft / initialTime) ) * 100;
  
  progressBarElement.style.width = progressPercentage + '%';
  progressBarTextElement.innerHTML = timeLeft + 's';
}

function tick() {
  timeLeft = timeLeft - 1;
  if(timeLeft <= 0) {
    clearInterval(interval); //Stops interval
  }
  
  render(); //Updates html
}

function startProgressBar() {
   interval = setInterval(tick, 1000); //Will call tick every second
   render();
}

startProgressBar();
html {font-family: -apple-system,BlinkMacSystemFont,"Segoe UI","Roboto","Oxygen","Ubuntu","Cantarell","Fira Sans","Droid Sans","Helvetica Neue",sans-serif;}

.progress-bar-continer {
  height: 80px;
  width: 100%;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #406086;
}

.progress-bar {
  background-color: #1b3e80;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 0%;
  transition: width 1s; /* Makes progressBar smooth */
  transition-timing-function: linear; /* Try to remove this line for more tick tock feel */
  
}

.progress-bar-text {
  color: white;
  font-size: 24px;
  font-weight: 700;
  position: relative;
  z-index: 1;
}
<div class="progress-bar-continer">
  <div class="progress-bar" id="progress-bar"></div>
  <div class="progress-bar-text" id="progress-bar-text"></div>
<div>

Try to understand the code best you can and you will have no problems adding any features you want on top of it. All the fancy stuff will come later with experience.

almost 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda