Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The simple solution (I have marked my changes with <code>// ***</code>:</p> <pre><code>// *** var totalPages; function populatePagesArea() { // there was some code before the for loop // *** totalPages = pages.length; for (var i=0, l=pages.length; i&lt;l; i++) { addToPagesArea(pages[i], ""); } // some code after... } function addToPages(file, front) { // there was some code before.. reader = new FileReader(); reader.onload = (function (theDiv) { return function (evt) { var backgroundimage = "url(" + evt.target.result + ")"; theDiv.css("background-image", backgroundimage); var sizeSettings = getSizeSettingsFromPage(file, calculateRatio); // *** Check to see if we're done after every load checkPagesReady(); }; }(imageDiv)); // step#3 execute file reader reader.readAsDataURL(file); // there was some code after.. } // *** Call getMajorityPageSize() here, only after all pages have loaded. function checkPagesReady() { if (pageSizes.length &gt;= totalPages) getMajorityPageSize(); } </code></pre> <hr> <p>The better solution if you're going to be dealing with more asynchronous things later on would be to refactor your code using <a href="https://www.google.com/#q=promises+javascript" rel="nofollow">promises</a>. Promises is an API designed for dealing with asynchronous programming in a systematic and organized way. It'll make your life a lot easier if you're going to be doing more async work. There's a lot of free libraries that support promises, one of the major players is <a href="https://github.com/kriskowal/q" rel="nofollow">Q.js</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload