title: Javascript: How to update a progress bar in a 'for' loop
body: <p>I am have an issue with a JS script I am trying to put together.  I have an HTML table with somewhere in the neighborhood of 300 rows in it.  I have made a sort function that will make the table headers clickable and launch my sort function. I would like to integrate a progress bar because in larger tables (500 - 1000 rows) after a header is clicked the table takes a bit of time to sort (IE is a big offender). The progress bar would tell them how much time remains before the sort is complete.  The method I had in mind was a div element that I would resize based on the progression of the sort loop. The problem is that I can't seem to figure out how to integrate such a routine into my loop.  </p>
  
  <p>I've researched the issue and taken note of this: <a href="https://stackoverflow.com/questions/14774245/how-to-change-progress-bar-in-loop">How to change progress bar in loop?</a>
  and this: <a href="https://stackoverflow.com/questions/7690465/using-settimeout-to-update-progress-bar-when-looping-over-multiple-variables">Using setTimeout to update progress bar when looping over multiple variables</a></p>
  
  <p>The second topic has a few demos that do essentially what I would like to do as far as the progress bar goes.  However, anytime I try to implement the solutions shown in those two posts I either:</p>
  
  <p>A - Crash the browser</p>
  
  <p>B - Progress bar appears to work, but goes from 0 - 100% instantly, not progressively.</p>
  
  <p>I am hoping someone can lead me in the right direction on what to do.  This table sort progress indicator must be done using native JS because the contents must be available offline, hence I can't include any jQuery libraries via CDN and bloating the document with the entire jQuery library isn't desired.</p>
  
  <p>I've created a JS fiddle with my code in it.  I've stripped out what I had for progress bar code because I kept crashing the browser so all that is there as far as scripts go is the sorting-related code. <a href="http://jsfiddle.net/jnBmp/3/" rel="noreferrer">jsfiddle</a></p>
  
  <p>Here is the JS itself: 
  <code></p>
  
  <pre><code>//Change this variable to match the "id" attribute of the
  //table that is going to be operated on.
  var tableID = "sortable";
  
  /**
   * Attach click events to all the &lt;th&gt; elements in a table to 
   * call the tableSort() function. This function assumes that cells  
   * in the first row in a table are &lt;th&gt; headers tags and that cells
   * in the remaining rows are &lt;td&gt; data tags.
   *
   * @param table The table element to work with.
   * @return void
   */
  function initHeaders(table) {
      //Get the table element
      table = document.getElementById(table);
      //Get the number of cells in the header row
      var l = table.rows[0].cells.length;
      //Loop through the header cells and attach the events
      for(var i = 0; i &lt; l; i++) {
          if(table.rows[0].cells[i].addEventListener) { //For modern browsers
              table.rows[0].cells[i].addEventListener("click", tableSort, false);
          } else if(table.rows[0].cells[i].attachEvent) { //IE specific method
              table.rows[0].cells[i].attachEvent("onclick", tableSort);
          }
      }
  }
  
  /**
   * Compares values in a column of a table and then sorts the table rows.
   * Subsequent calls to this function will toggle a row between ascending
   * and descending sort directions.
   *
   * @param e The click event passed in from the browser.
   * @return mixed No return value if operation completes successfully, FALSE on error.
   */
  function tableSort(e) { 
  
      /**
       * Checks to see if a value is numeric.
       *
       * @param n The incoming value to check.
       * @return bool TRUE if value is numeric, FALSE otherwise.
       */
      tableSort.isNumeric = function (n) {
          var num = false;
          if(!isNaN(n) &amp;&amp; isFinite(n)) {
              num = true;
          }
          return num;
      }
  
      //Get the element from the click event that was passed.
      if(e.currentTarget) { //For modern browsers
          e = e.currentTarget;
      } else if(window.event.srcElement) { //IE specific method
          e = window.event.srcElement;
      } else {
          console.log("Unable to determine source event. Terminating....");
          return false;
      }
  
      //Get the index of the cell, will be needed later
      var ndx = e.cellIndex;
  
      //Toggle between "asc" and "desc" depending on element's id attribute
      if(e.id == "asc") {
          e.id = "desc";
      } else {
          e.id = "asc";
      }
  
      //Move up from the &lt;th&gt; that was clicked and find the parent table element.
      var parent = e.parentElement;
      var s = parent.tagName;
      while(s.toLowerCase() != "table") {
          parent = parent.parentElement;
          s = parent.tagName;
      }
  
      /*
      Executes two different loops.  A "for" loop to control how many
      times the table rows are passed looking for values to sort and a
      "while" loop that does the actual comparing of values.  The "for"
      loop also controls how many times the embedded "while" loop will
      run since each iteration with force at least one table row into 
      the correct position.   
      */
  
      //var interval = setInterval( function () { progress.updateProgress() } , 100);
      var rows = parent.tBodies[0].rows.length; //Isolate and count rows only in the &lt;tbody&gt; element.
      if(rows &gt; 1) {  //Make sure there are enough rows to bother with sorting
          var v1; //Value 1 placeholder
          var v2; //Value 2 placeholder
          var tbody = parent.tBodies[0];  //Table body to manipulate
          //Start the for loop (controls amount of table passes)
          for(i = 0; i &lt; rows; i++) {
              var j = 0;  //Counter for swapping routine
              var offset = rows - i - 1;  //Stops next loop from overchecking
  
              // WANT TO UPDATE PROGRESS BAR HERE
  
              //Start the while loop (controls number of comparisons to make)
              while(j &lt; offset) {             
  
                  //Check to make sure values can be extracted before proceeding
                  if(typeof tbody.rows[j].cells[ndx].innerHTML !== undefined &amp;&amp; typeof tbody.rows[j + 1].cells[ndx].innerHTML !== undefined) {
  
                      //Get cell values and compare
                      v1 = tbody.rows[j].cells[ndx].innerHTML;
                      v2 = tbody.rows[j + 1].cells[ndx].innerHTML;
                      if(tableSort.isNumeric(v1) &amp;&amp; tableSort.isNumeric(v2)) {
                          //Dealing with two numbers
                          v1 = new Number(v1);
                          v2 = new Number(v2);
                          if(v1 &gt; v2) {
                              if(e.id == "asc") { //v1 moves down
                                  tbody.insertBefore(tbody.rows[j + 1], tbody.rows[j]);
                              }
                          } else {
                              if(e.id == "desc") { //v1 moves down
                                  tbody.insertBefore(tbody.rows[j + 1], tbody.rows[j]);
                              }
                          }
                      } else if(tableSort.isNumeric(v1) &amp;&amp; !tableSort.isNumeric(v2)) {
                          //v2 is a string, v1 is a number and automatically wins
                          if(e.id == "asc") { //v1 moves down
                              tbody.insertBefore(tbody.rows[j + 1], tbody.rows[j]);
                          }
                      } else if(!tableSort.isNumeric(v1) &amp;&amp; tableSort.isNumeric(v2)) {
                          //v1 is a string, v2 is a number and automatically wins
                          if(e.id == "desc") { //v1 moves down
                              tbody.insertBefore(tbody.rows[j + 1], tbody.rows[j]);
                          }
                      } else {
                          //Both v1 and v2 are strings, use localeCompare()
                          if(v1.localeCompare(v2) &gt; 0) {
                              if(e.id == "asc") { //v1 moves down
                                  tbody.insertBefore(tbody.rows[j + 1], tbody.rows[j]);
                              }
                          } else {
                              if(e.id == "desc") { //v1 moves down
                                  tbody.insertBefore(tbody.rows[j + 1], tbody.rows[j]);
                              }
                          }
                      }
                      j++;
                  } else {
                      console.log("One of the values turned up undefined");
                  }
              }
          }
      }
  }
  
  //Wait until DOM is ready and then initialize the table headers.
  window.onload = function () {
      initHeaders(tableID);
  }
  </code></pre>
  
  <p></code></p>
  
  <p>Thanks in advance to anyone who can point me in the right direction.</p>
  
  <p><strong>-----
  EDIT:
  -----</strong> Okay so after reading the answers here and making some hefty modifications to how I was going about things I have come up with a much better solution.  The progress bar isn't exactly what I wanted, but it is close.  (Although I believe that it is showing up on the page just as the sorting is getting ready to finish up.)</p>
  
  <p>I modified my loop to do a O(n log n) quick sort and instead of modifying the DOM directly I instead isolate the table rows to sort and copy them into an array.  I then do the sort directly on the array and once it is finished I rebuild the rows and then remove the old rows and append the new ones in.  The sort time has been reduced significantly.</p>
  
  <p>Have a look: <a href="http://jsfiddle.net/jnBmp/5/" rel="noreferrer">http://jsfiddle.net/jnBmp/5/</a></p>
  
  <p>And here is the new JS code:</p>
  
  <pre><code>//Change this variable to match the "id" attribute of the
  //table that is going to be operated on.
  var tableID = "sortable";
  
  /**
   * Attach click events to all the &lt;th&gt; elements in a table to 
   * call the tableSort() function. This function assumes that cells  
   * in the first row in a table are &lt;th&gt; headers tags and that cells
   * in the remaining rows are &lt;td&gt; data tags.
   *
   * @param table The table element to work with.
   * @return void
   */
  function initHeaders(table) {
      //Get the table element
      table = document.getElementById(table);
      //Get the number of cells in the header row
      var l = table.rows[0].cells.length;
      //Loop through the header cells and attach the events
      for(var i = 0; i &lt; l; i++) {
          if(table.rows[0].cells[i].addEventListener) { //For modern browsers
              table.rows[0].cells[i].addEventListener("click", tableSort, false);
          } else if(table.rows[0].cells[i].attachEvent) { //IE specific method
              table.rows[0].cells[i].attachEvent("onclick", tableSort);
          }
      }
  }
  
  
  function tableSort(e) { 
  
      var runs = 0;
      var pix = 0;
      var ndx = 0;
      var dir = "right";
      var interval = false;
  
      //Get the element from the click event that was passed.
      if(e.currentTarget) { //For modern browsers
          e = e.currentTarget;
      } else if(window.event.srcElement) { //IE specific method
          e = window.event.srcElement;
      } else {
          console.log("Unable to determine source event. Terminating....");
          return false;
      }
  
      //Get the index of the cell, will be needed later
      ndx = e.cellIndex;
  
      //Toggle between "asc" and "desc" depending on element's id attribute
      if(e.id == "asc") {
          e.id = "desc";
      } else {
          e.id = "asc";
      }
  
      //Move up from the &lt;th&gt; that was clicked and find the parent table element.
      var parent = e.parentElement;
      var s = parent.tagName;
      while(s.toLowerCase() != "table") {
          parent = parent.parentElement;
          s = parent.tagName;
      }
  
      //Get the rows to operate on as an array
      var rows = document.getElementById("replace").rows;
      var a = new Array();
      for(i = 0; i &lt; rows.length; i++) {
          a.push(rows[i]);
      }
  
      //Show progress bar ticker
      document.getElementById("progress").style.display = "block";
  
      /**
       * Show the progress bar ticker animation
       *
       * @param pix The current pixel count to set the &lt;div&gt; margin at.
       */
      function updateTicker(pix) {
  
                  var tick = document.getElementById("progressTicker");
                  document.getElementById("progressText").style.display = "block";
                  document.getElementById("progressText").innerHTML = "Sorting table...please wait";
                  if(dir == "right") {
                      if(pix &lt; 170) {
                          pix += 5;
                          tick.style.marginLeft = pix + "px";
                      } else {
                          dir = "left";
                      }
                  } else {
                      if(pix &gt; 0) {
                          pix -= 5;
                          tick.style.marginLeft = pix + "px";
                      } else {
                          dir = "left";
                      }
                  }
                  interval = window.setTimeout( function () { updateTicker(pix); }, 25);
      }
      updateTicker(pix);
  
      /**
       * Checks to see if a value is numeric.
       *
       * @param n The incoming value to check.
       * @return bool TRUE if value is numeric, FALSE otherwise.
       */
      isNumeric = function (n) {
          var num = false;
          if(!isNaN(n) &amp;&amp; isFinite(n)) {
              num = true;
          }
          return num;
      }
  
      /**
       * Compares two values and determines which one is "bigger".
       *
       * @param x A reference value to check against.
       * @param y The value to be determined bigger or smaller than the reference.
       * @return TRUE if y is greater or equal to x, FALSE otherwise
       */
      function compare(x, y) {
          var bigger = false;
          x = x.cells[ndx].textContent;
          y = y.cells[ndx].textContent;
          //console.log(e.id);
          if(isNumeric(x) &amp;&amp; isNumeric(y)) {
              if(y &gt;= x) {
                  bigger = (e.id == "asc") ? true : false;
              } else {                
                  bigger = (e.id == "desc") ? true : false;
              }
          } else {
              if(y.localeCompare(x) &gt;= 0) {
                  bigger = (e.id == "asc") ? true : false;
              } else {                
                  bigger = (e.id == "desc") ? true : false;
              }
          }
          return bigger;
      }   
  
      /**
       * Performs a quicksort O(n log n) on an array.
       *
       * @param array The array that needs sorting
       * @return array The sorted array.
       */
      function nlognSort(array) {
          runs++
          if(array.length &gt; 1) {
              var big = new Array();
              var small = new Array();
              var pivot = array.pop();
              var l = array.length;
              for(i = 0; i &lt; l; i++) {
                  if(compare(pivot,array[i])) {
                      big.push(array[i]);
                  } else {
                      small.push(array[i]);
                  }
              }
              return Array.prototype.concat(nlognSort(small), pivot, nlognSort(big));
          } else {
              return array;
          }
      }
  
  
      //Run sort routine  
      b = nlognSort(a);
  
      //Rebuild &lt;tbody&gt; and replace new with the old
      var tbody = document.createElement("tbody");
      var l = b.length;
      for(i = 0; i &lt; l; i++) {
          tbody.appendChild(b.shift());
      }
      parent.removeChild(document.getElementById("replace"));
      parent.appendChild(tbody);
      tbody.setAttribute("id","replace");
      setTimeout(function () {
          document.getElementById("progress").style.display = "none";
          document.getElementById("progressText").style.display = "none";
          clearTimeout(interval);
      },1500);
  }
  
  
  window.onload = function() {
      initHeaders(tableID);
  }
  </code></pre>
  
  <p>Thanks again everyone!!</p>