The Lodge is members-only design/dev videos and Office Hours.

Next Office Hours Session: "Implementing an SVG Icon System" Nov 30 - 6:00 PM Eastern

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); = randomPosition(0, 540) + “px”; = 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.

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed