JSFiddle - React, Tailwind, and code Playground

by princeofabyss

HTML

<div id='progressbar4'></div>

CSS

.progressbar {
  width: 100%;
  margin: 25px auto;
  border: solid 1px #000;
}

.progressbar .inner {
  height: 25px;
  animation: progressbar-countdown;
  animation-duration: 40s;

  /*-------------------------------*/
  animation-delay: -60s;
  /*-------------------------------*/

  animation-iteration-count: 1;
  animation-fill-mode: forwards;
  animation-play-state: paused;
  animation-timing-function: linear;
}

@keyframes progressbar-countdown {
  0% {
    width: 100%;
    background: #00f;
  }

  100% {
    width: 0%;
    background: #f00;
  }
}

JavaScript

function createProgressbar(id, duration, callback) {
	// We select the div that we want to turn into a progressbar
	var progressbar = document.getElementById(id);
	progressbar.className = 'progressbar';

	// We create the div that changes width to show progress
	var progressbarinner = document.createElement('div');
	progressbarinner.className = 'inner';

	// Now we set the animation parameters
	progressbarinner.style.animationDuration = duration;

	// Eventually couple a callback
	if (typeof(callback) === 'function') {
		progressbarinner.addEventListener('animationend', callback);
	}

	// Append the progressbar to the main progressbardiv
	progressbar.appendChild(progressbarinner);

	// When everything is set up we start the animation
	progressbarinner.style.animationPlayState = 'running';
}

addEventListener('load', function() {
	createProgressbar('progressbar4', '180s', function() {
		alert('180s progressbar is finished!');
	});
});