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

292
Views
Pause / re-initiate progressbar? Javascript and animationPlayState

I got myself a simple script that creates a progressbar which runs for 180 seconds. After that time the progressbar sends the browser to another website. View / run the code below. I added an onclick event to the progress bar for testing purposes. What I want; I want to be able to pause, reset and set a new running-time value. I tried using document.getElementById('progressbar1').style.animationPlayState = 'paused'; to pause the progressbar, but unfortunately nothing is happening. Do you guys have any clue on how to pause the progressbar?

function createProgressbar(id, duration, callback) {
  var progressbar = document.getElementById(id);
  progressbar.className = 'progressbar';
  var progressbarinner = document.createElement('div');
  progressbarinner.className = 'inner';
  progressbarinner.style.animationDuration = duration;
  if (typeof(callback) === 'function') {
    progressbarinner.addEventListener('animationend', callback);
  }
  progressbar.appendChild(progressbarinner);
  progressbarinner.style.animationPlayState = 'running';
}

addEventListener('load', function() {
  createProgressbar('progressbar1', '180s', function() {
    window.location.replace("gotomypage.html");
    
  });
  
document.getElementById('progressbar1').addEventListener("click", function() {
      console.log ('TEST - Stop Progressbar');
      document.getElementById('progressbar1').style.animationPlayState = 'paused';
  });

});
.progressbar {
  width: 500px;
  margin: 25px auto;
  border: solid 1px #000;
  position: absolute;
  right:25%;
  left:50%;
  margin-left:-250px;  
}
.progressbar .inner {
  height: 15px;
  animation: progressbar-countdown;
  animation-duration: 40s;
  animation-iteration-count: 1;
  animation-fill-mode: forwards;
  animation-play-state: paused;
  animation-timing-function: linear;
}
@keyframes progressbar-countdown {
  0% {
    width: 100%;
    background: #0F0;
  }
  100% {
    width: 0%;
    background: #F00;
  }
}       
<div id="progressbar1"></div>

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

0

You are selecting the wrong element to stop the animation! The animation is running on #progressbar1 .inner

function createProgressbar(id, duration, callback) {
  var progressbar = document.getElementById(id);
  progressbar.className = 'progressbar';
  var progressbarinner = document.createElement('div');
  progressbarinner.className = 'inner';
  progressbarinner.style.animationDuration = duration;
  if (typeof(callback) === 'function') {
    progressbarinner.addEventListener('animationend', callback);
  }
  progressbar.appendChild(progressbarinner);
  progressbarinner.style.animationPlayState = 'running';
}

addEventListener('load', function() {
  createProgressbar('progressbar1', '180s', function() {
    window.location.replace("gotomypage.html");
    
  });
  
document.getElementById('progressbar1').addEventListener("click", function() {
      console.log ('TEST - Stop Progressbar');
      document.getElementById('progressbar1').getElementsByClassName('inner')[0].style.animationPlayState = 'paused';
  });

});
.progressbar {
  width: 500px;
  margin: 25px auto;
  border: solid 1px #000;
  position: absolute;
  right:25%;
  left:50%;
  margin-left:-250px;  
}
.progressbar .inner {
  height: 15px;
  animation: progressbar-countdown;
  animation-duration: 40s;
  animation-iteration-count: 1;
  animation-fill-mode: forwards;
  animation-play-state: paused;
  animation-timing-function: linear;
}
@keyframes progressbar-countdown {
  0% {
    width: 100%;
    background: #0F0;
  }
  100% {
    width: 0%;
    background: #F00;
  }
}
<div id="progressbar1"></div>

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!