title: Javascript Printing a page while looping through objects
body: <p>I have a webpage that I'm using to print 'pages' of data as PDF files via the firefox 'Print to File' printer on my laptop.  The code I'm invoking is as follows:</p>
  
  <pre><code>   document.body.controls.cmdPrint.click = function ()                              // Create a function that will be called when this object is clicked upon
     {if (parseInt(document.body.controls.page.innerHTML) !== 0)                      //  If we are not on the Front Cover
      {return false;}                                                                 //   Function complete: Abnormal Termination
      document.body.controls.style.pointerEvents = 'none';                            //  Lock down the controls so they cannot be interfered with
      do                                                                              //  Do...
      {window.print();                                                                //   Print this page
  //   document.body.sleep(); // Removed as this does not work as expected (see below...)
      } while (document.body.controls.cmdNext.click())                                //   ...while we are able to advance.
      document.body.controls.style.pointerEvents = '';                                //  Release the controls lockout
      this.blur();                                                                    //  Blur the focus
      return true;};                                                                  // Function complete: Normal Termination
  </code></pre>
  
  <p>When executing, the pages flip as expected (as the cmdNext.click() function returns a true when successful and a false when it's on the last page and trying to advance), but it runs too quickly.  Namely every odd page is caught out, as the 'printer' is unavailable....the window.print() is being released before the printer is ready for the next page.</p>
  
  <p>I tried slowing down execution by adding a reference to a secondary function within the loop (now commented out), but this just locks up the CPU and keeps the printer from processing in another thread....so it's not a valid solution.  This function (which I wrote but did not provide the expected cushioning to allow odd pages to print) is as follows.</p>
  
  <pre><code>   document.body.sleep = function (delay)                                           // Create a new function
     {delay = delay || 1;                                                             //  Default to a delay of 1 second
      var timestamp = new Date();                                                     //  Get current time
      timestamp = new Date(timestamp.getTime() + (delay * 1000));                     //  Add in the delay (in seconds)
      while (new Date() &lt; timestamp) {}                                               //  While we are waiting for the delay, do nothing
      return true;};                                                                  // Function complete: Normal Termination
  </code></pre>
  
  <p>Basically what I need is a way to hold loop execution long enough to let the 'Print to File' to go through before the next window.print() is called.  Using the sleep function above also keeps the window.print() from running even though (when I was trying to use it as a fix) I had put the call to this function immediately after the window.print() command.</p>
  
  <p>So I ask, can anyone here supply a fix for this project so I don't have to manually cycle through each page (which could get annoyingly temporally expensive with a page count above 10)?</p>
  
  <p>As it is currently, when the second page tries to print, I get the popup window holding an error from firefox: "Printer Error - Some printing functionality is not currently available."  Tracking this down lead to PERR_NOT_AVAILABLE....probably because the printer (Print to File) is busy printing the prior page....so I just need to wait for that to resolve before going to the next printing.  An error handler to catch this PERR_NOT_AVAILABLE instead of letting it bounce to the user (me) as a popup window that has to be clicked out of would be nice, though a spammy way of getting the pages to print in sequence as quickly as the Print to File system can process them.</p>
  
  <p>If window.print() actually returned an error in this condition, I could just rerun the command...</p>