Grow your CSS skills. Land your dream job.

Stopping a loop

  • # December 19, 2012 at 6:45 am

    Hello beautiful people!

    Right, so I’m working on some code for a tiny little game, and I was wondering if it’s possible to stop a loop which generates elements. When they the meteors collide with “felix” (I have function for that), a game over overlay is shown, but I need to stop that loop from running forever.

    function createMeteor() {

    for(var i; i=0; i < 1; i++) {
    var meteor = document.createElement(“div”);
    meteor.className = “meteor”;
    document.getElementById(“game”).appendChild(meteor);
    meteor.style.left = randomPosition(0, 540) + “px”;
    meteor.style.top = document.getElementById(“game”).offsetHeight + “px”;
    }
    }

    Is it possible to stop this function createMeteor or the loop inside it?

    Thanks in advance!

    # December 19, 2012 at 9:12 am

    I feel like I’m missing something here. What is the point of having a for loop when you only have one element?

    # December 19, 2012 at 9:32 am

    Maybe because it’s a game he only wants it to run once in this particular case but may need it to run more than once somewhere else. I have no idea.

    # December 19, 2012 at 8:11 pm

    Use a ‘while’ loop, not a for loop

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".