Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a solution:</p> <pre><code>function appendToSelect() { $("#mySelect").children().remove(); $("#mySelect").html( '&lt;option selected value="'+obj.data[0].value+'"&gt;' + obj.data[0].name + '&lt;/option&gt;' ); obj.data.splice(0, 1); // we only want remaining data var appendOptions = function() { var dataChunk = obj.data.splice(0, 10); // configure this last number (the size of the 'chunk') to suit your needs for(var i = 0; i &lt; dataChunk.length; i++) { $("#mySelect").append( '&lt;option value="' + dataChunk[i].value + '"&gt;' + dataChunk[i].name + '&lt;/option&gt;' ); } if(obj.data.length &gt; 0) { setTimeout(appendOptions, 100); // change time to suit needs } }; appendOptions(); // kicks it off } </code></pre> <p>Not as elegant as <a href="https://stackoverflow.com/questions/210821/giving-brief-control-back-to-the-browser-during-intensive-javascript-processing#210968">@Borgar's</a> solution, but you get the idea. Basically, I am doing the same thing, but all in your one function rather than breaking it into a higher-order function like he does. I like his solution, but if you don't, perhaps this will work for you.</p> <hr> <p>EDIT: For those that don't immediately see it, one of the main differences between this solution and <a href="https://stackoverflow.com/questions/210821/giving-brief-control-back-to-the-browser-during-intensive-javascript-processing#210968">@Borgar's</a> is that this solution allows you to set the size of the 'chunks' of data that is processed between each timeout. <a href="https://stackoverflow.com/questions/210821/giving-brief-control-back-to-the-browser-during-intensive-javascript-processing#210968">@Borgar's</a> times-out after <em>every single member</em> of the array is processed. If I get time, I will try to create a higher-order function to handle this so it is more elegant. No promises though! ;)</p> <hr> <p>EDIT: So, here is my adaptation of <a href="https://stackoverflow.com/questions/210821/giving-brief-control-back-to-the-browser-during-intensive-javascript-processing#210968">@Borgar's</a> solution, which allows for setting a 'chunk' size and configuring the timeout value more easily:</p> <pre><code>function incrementallyProcess(workerCallback, data, chunkSize, timeout, completionCallback) { var itemIndex = 0; (function() { var remainingDataLength = (data.length - itemIndex); var currentChunkSize = (remainingDataLength &gt;= chunkSize) ? chunkSize : remainingDataLength; if(itemIndex &lt; data.length) { while(currentChunkSize--) { workerCallback(data[itemIndex++]); } setTimeout(arguments.callee, timeout); } else if(completionCallback) { completionCallback(); } })(); } function appendToSelect() { $("#mySelect").children().remove(); $("#mySelect").html( '&lt;option selected value="' + obj.data[0].value + '"&gt;' + obj.data[0].name + '&lt;/option&gt;' ); obj.data.splice(0,1); // we only want remaining data incrementallyProcess(function(data) { $("#mySelect").append( '&lt;option value="' + data.value + '"&gt;' + data.name + '&lt;/option&gt;' ); }, obj.data, 10, 100, removeAnimatedGifFunction); // last function not required... } </code></pre> <p>Hope that helps - I think this combines the best of both solutions. <strong>Notice</strong>, the second anonymous function no longer uses the index value, but simply passes in the entire object (with the value and name properties); that makes it a bit cleaner, since the index of the current object really isn't <em>usually</em> that useful when iterating over things, IMO.</p> <p>I am sure there are still things that could be done to make this even better, but that is left as an exercise for the reader. ;)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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