cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div>
  <button id="btnCreateProgressBar" data-number-of-bars="0">
    Create New Progress Bar
  </button>
</div>

<div id="progressBarContainer">
</div>
            
          
!
            
              /*******************************************************************************
Technical Interview
Create Dynamic Progress Bars

Robert Sogomonian
Dec 18, 2015

Updated: Mar 12, 2016

1. Create a progress bar that fills from 0% - 100% in 5 seconds.
2. Add buttons that control starting, stopping, and resetting the progress bar.
3. Create button that handles creating new progress bars each time it is pressed.
   Each progress bar should have its own control buttons, and no more than three
   progress bars should be able to be created.
*******************************************************************************/

(function(window, undefined){
  // 
  var btnCreateProgressBar = document.getElementById('btnCreateProgressBar'),
      divProgressBarContainer = document.getElementById('progressBarContainer'),
      maxAllowedProgressBars = 3;
  
  
  // When button is clicked, add a new progress bar to the page
  btnCreateProgressBar.addEventListener("click", function () {
    var progressBarCount = btnCreateProgressBar.dataset.numberOfBars;
    
    // If we don't already have a full set of progress bars
    if (progressBarCount < maxAllowedProgressBars) {
      var container = createNewProgressBar(progressBarCount);
      
      // Create controls
      addControl("start", progressBarCount, container);
      addControl("stop", progressBarCount, container);
      addControl("reload", progressBarCount, container);
      
      // Increment count of progress bars currently on the page
      btnCreateProgressBar.dataset.numberOfBars++;
    }
  });
  
  function createNewProgressBar(barId) {
    // Create a new progress bar and container
    var progressBarDiv = document.createElement("div"),
        newProgressBar = document.createElement("progress");

    // Create a container and progress bar to add to the page
    progressBarDiv.setAttribute("class", "progressContainer");
    newProgressBar.setAttribute("id", "progressBar" + barId);
    newProgressBar.setAttribute("value", "0");
    newProgressBar.setAttribute("max", "100");
    
    // Append all new elements to the page
    divProgressBarContainer.appendChild(progressBarDiv);
    progressBarDiv.appendChild(newProgressBar);
    
    return(progressBarDiv);
  }
  
  function addControl(type, barId, container) {
    var controlElement = document.createElement("button"),
        textLabel = "";
    
    controlElement.appendChild(document.createTextNode(""));
    controlElement.setAttribute("id", type + barId);
    controlElement.setAttribute("class", "barControl");
    container.appendChild(controlElement);
    
    // Add Controller Text and Functionality Depending on Button Type
    switch(type) {
      case 'start':
        controlElement.innerHTML = "&#9658;"; // Play Arrow
        setStartController(controlElement, barId);
        break;
      case 'stop':
        controlElement.innerHTML = "\u2016"; // Pause Bars
        setStopController(controlElement, barId);
        break;
      case 'reload':
        controlElement.innerHTML = "&#8635;"; // Reset Circular Arrow
        setResetController(controlElement, barId);
        break;
    }
  }
  
  function setStartController(element, id) {
    var progressBar = document.getElementById("progressBar"+id),
        startButton = document.getElementById("start"+id);
      element.addEventListener("click", function() {
        // If a timer has not already been set on this button
        if(!startButton.dataset.timerId) {
          startButton.dataset.timerId = setInterval(function() {
            if(progressBar.value < progressBar.max) {
              progressBar.value += 1;
            } else {
              clearInterval(startButton.dataset.timerId);
              delete startButton.dataset.timerId;
            }
          }, 50);
        }
      });
  }
  
  function setStopController(element, id) {
    var startButton = document.getElementById("start"+id);
    element.addEventListener("click", function() {
      clearInterval(startButton.dataset.timerId);
      delete startButton.dataset.timerId;
    });
  }
  
  function setResetController(element, id) {
    var progressBar = document.getElementById("progressBar"+id),
        startButton = document.getElementById("start"+id);
    element.addEventListener("click", function() {
      clearInterval(startButton.dataset.timerId);
      delete startButton.dataset.timerId;
      progressBar.value = 0;
    });
  }
})(this);
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console