Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What the code does (on a high level) is this:</p> <ol> <li><p>When you click the button, the input data is set up (<code>testCSVFunc()</code> - the loop makes sure that empty artist cells are filled with the previous row's artist cell).</p></li> <li><p>Then it (<code>testCSVFunc()</code>) calls <code>enterFrame()</code></p></li> <li><p><code>enterFrame()</code> calls finalCSV repeatedly, adding lines of "Artist -- Title" to a text field.</p></li> <li><p>... until the specified time has passed. I.e. 1000/<strong>24</strong> - 2 - 2 = approx. 38 msecs - which corresponds to slightly less than the duration of a frame at <strong>24</strong> frames per second. Not sure why "- 2 - 2", but the idea is to leave some time each frame for Flash to do other things (like rendering the stage).</p></li> <li><p>When 38 msecs have been spent processing, it stops (the <code>return</code> statement) - and will do nothing more until you click again (at which point it will - by being called from <code>testCSVFunc()</code> - continue where it left off - at <code>savedIndex</code>). <code>testCSVFunc</code> will continue to process data, until <code>i &lt; n</code> is no longer true - i.e., when there are no more items left in the CSV.</p></li> </ol> <p>I'm guessing the idea is that testCSVFunc should only be called once. It sets up all the data (calling it twice adds the same data to the "todo list" once more). Then finalCSV is supposed to process that data and add it to the text field.</p> <p>Which means <code>enterFrame()</code> isn't supposed to be called directly by testCSVFunc (which is triggered by the click) - it's meant to be called once every frame (hence the name), until all the data has been processed.</p> <p>I.e. rather than calling <code>enterFrame()</code>, you set it up as an ENTER_FRAME event handler:</p> <pre><code>addEventListener(Event.ENTER_FRAME, enterFrame); </code></pre> <p>In that way, when clicking, the "thread" will start, and <code>enterFrame()</code> will be executed each frame.</p> <p>Remember to add an <code>event:Event</code> parameter to the <code>enterFrame()</code> declaration - even though you don't use it. Otherwise you'll get an error about the missing parameter:</p> <pre><code>function enterFrame(event:Event):void { ... } </code></pre> <p>When done processing (when the loop no longer iterates, because <code>i &lt; n</code> is no longer true), it would be a good idea to disable the event handler again, i.e.:</p> <pre><code>// done removeEventListener(Event.ENTER_FRAME, enterFrame); trace('done'); </code></pre> <p>Otherwise it will keep calling the "worker method", even though it has no more work to do.</p> <p>All in all, this means that each frame, most of the frame time (38 msecs) will be spent filling out the text field, after which the rendering of the stage can execute. I'd probably set <code>allowedTime</code> somewhat lower than that.</p> <p>As weltraumpirat says, there are generalized implementations that are probably more efficient and likely to take care of Flash's idiosyncracies, but as an experiment in working out how "pseudo-threading" actually works, it's a good learning exercise.</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