function initiateRound(roundNumber) {

    // Closing over roundNumber in this context is confusing - but important.

    // Without access to the environment variable roundNumber the rounds would not advance properly.
    // It's recursive, but also in an anonymous function, sent on "timeout", 
    // only to return after ROUND_COOLDOWN seconds. 

    // No matter, this (kind of horrifying to debug) technique works.
    var closureFunction = function() {

        if(roundNumber < NUMBER_OF_ROUNDS) {
            // Create the moles 
            for(var i = 0; i < MOLES_PER_ROUND; i++) new Mole().emerge();

            // Next round, using our precious closed-over parameter 
            initiateRound(roundNumber + 1);
        }
        else {
            endGame();
        }
    };

    // Set it and forget it.
    setTimeout(closureFunction, ROUND_COOLDOWN * 1000);
}
/**
* Use a closure and the event loop to act every ROUND_COOLDOWN seconds
*/
Use a closure and the event loop to act every ROUND_COOLDOWN seconds
Use a closure and the event loop to act every ROUND_COOLDOWN seconds
function initiateRound(roundNumber) {
    var closureFunction = function() {
        if(roundNumber < NUMBER_OF_ROUNDS) {
            for(var i = 0; i < MOLES_PER_ROUND; i++) new Mole().emerge();
            initiateRound(roundNumber + 1);
        }
        else {
            endGame();
        }
    };
    setTimeout(closureFunction, ROUND_COOLDOWN * 1000);
}
function initiateRound ( roundNumber ) { var closureFunction = function ( ) { if ( roundNumber < NUMBER_OF_ROUNDS ) { for ( var i = 0 ; i < MOLES_PER_ROUND ; i ++ ) new Mole ( ) . emerge ( ) ; initiateRound ( roundNumber + 1 ) ; } else { endGame ( ) ; } } ; setTimeout ( closureFunction , ROUND_COOLDOWN * 1000 ) ; }
Use a closure and the event loop to act every ROUND_COOLDOWN seconds
Use a closure and the event loop to act every ROUND_COOLDOWN seconds
// Closing over roundNumber in this context is confusing - but important. // Without access to the environment variable roundNumber the rounds would not advance properly. // It's recursive, but also in an anonymous function, sent on "timeout",  // only to return after ROUND_COOLDOWN seconds.  // No matter, this (kind of horrifying to debug) technique works. // Create the moles  // Next round, using our precious closed-over parameter  // Set it and forget it.