Javascript loop - waiting on value
<p>Having a nightmare with a function that's finishing before all the code has run. Am trying to build in a counter, and only returning when the code is finished.</p>

<p>I've emulated this like so (I know it's not fantastic, but if someone could point me along the right lines, I'd be very grateful):</p>

<pre><code>//I want this to alert "Done"
alert(timerCheck());

function timerCheck() {
    var finished;
    var myLoop = 5;
    for (i = 0; i &lt; myLoop; i++) {
        //This is emulating the slow code
        window.setTimeout(checkFinished, 900);
        alert(i);
    }
    function checkFinished() {
        //I originally had the "return here, before realising my error
        finished = true;
    }
    if (finished) {
        //This is where my problem is 
        return "done";
    }
}
</code></pre>

<p>Like I said, a much simplified example - if someone could point out the mistake, it'd save me a lot of hassle!</p>
