Home > Net >  How to add timer inside the progress bar
How to add timer inside the progress bar

Time:09-28

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 ></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>

CodePudding user response:

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 >
  <div  id="progress-bar"></div>
  <div  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.

  • Related