Is there any way of adapting this name-generating loop to run for a minimum number of seconds?
<p>I'm using the following code (which has some Frog VLE API code in it; hopefully that's not too relevant) to pick a random student from a list.</p>

<p>This works well, but sometimes - given its random nature - it only runs for a very brief period of time. Would it be possible to loop this a number of times first, to ensure that it runs for X period of time as a minimum?</p>

<pre><code>var Count = 0;

/* construct the array of people */
for (var i in data.users) {
    for (var n = 0; n &lt; data.users[i].Quantity; n++) {
        var TempObj = { 'Count' : Count, 'Student_ID' : i, 'Student_Name' : data.users[i].Student_Name };
        RewardPurchases.PurchasesArray[Count] = TempObj;
        Count++;
    }
}
</code></pre>

<p>... more code here, nothing relevant to the way the script works ...</p>

<pre><code>$('button#random').click( function() {

    /* first things first, play a drum-roll when the button is clicked! */
    $('embed').remove();
    $('body').append('&lt;embed src="/user/74/177499.wav" autostart="true" hidden="true" loop="true"&gt;');

    /* take the RewardPurchases.PurchasesArray and sort it so that there's no particular order */
    RewardPurchases.PurchasesArray.sort(function() {return 0.5 - Math.random()})

    /* remove the winner class for consecutive re-rolls */
    $display.removeClass( "winner" );
    $display.addClass( "spinner" );

    /* determine the number of people in the array and calculate a random winner */
    var total = RewardPurchases.PurchasesArray.length,
        selected = Math.floor( Math.random() * total ),
        i = 0;

    /* work out how long each name should appear for, dependent upon how many people are in the array */
    var timeout = ( 15000 / total );

    /* run through the array of people ... */
    for (i=0; i&lt;total; i++) {

        setTimeout((function(i){
            return function(){
                console.log( "timeout", i );

                /* ... if the person in the array is a valid person! then display their name */
                if (typeof RewardPurchases.PurchasesArray[i] === 'object' &amp;&amp; typeof RewardPurchases.PurchasesArray[i] !== null) {
                    $display.text( "[" + RewardPurchases.PurchasesArray[i].Count + "] " + RewardPurchases.PurchasesArray[i].Student_Name.toUpperCase() );

                    /* if we encounter the winner, play a cheering wav and change the class of the display so that they appear in big, bold, red text */
                    if( i === selected ) {
                        $('embed').remove();
                        $('body').append('&lt;embed src="/user/74/177086.wav" autostart="true" hidden="true" loop="false"&gt;');

                       $display.addClass( "winner" );
                    }
                }
            };
        }(i)), i*timeout);

        /* if the winner has been found, break the loop */
        if( i === selected ) {
            break;
        }
    }
} );
</code></pre>

<p>Thanks in advance,</p>
