55

When this variable becomes a certain amount i want the loop to stop, but i keep getting the error, "Uncaught SyntaxError: Illegal break statement".

function loop() {
    if (isPlaying) {
        jet1.draw();
        drawAllEnemies();
        requestAnimFrame(loop);
        if (game==1) {

            break;

        }

    }
} 
lehermj
  • 916
  • 4
  • 9
  • 20

5 Answers5

85

break is to break out of a loop like for, while, switch etc which you don't have here, you need to use return to break the execution flow of the current function and return to the caller.

function loop() {
    if (isPlaying) {
        jet1.draw();
        drawAllEnemies();
        requestAnimFrame(loop);
        if (game == 1) {
           return
        }
    }
}

Note: This does not cover the logic behind the if condition or when to return from the method, for that we need to have more context regarding the drawAllEnemies and requestAnimFrame method as well as how game value is updated

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • 1
    oh, duhh... i'm new to all of this, but thank you very much for your help! – lehermj Mar 21 '14 at 02:34
  • Doesn't actually exit the loop at all. requestAnimFrame will be called regardless of the value of `game`, causing `loop` to run forever. – Bart Mar 21 '14 at 02:34
  • that will depend on how `requestAnimFrame` is implemented and how the callback `loop` is called... if there is an conditional call to the callback it will exit – Arun P Johny Mar 21 '14 at 02:35
  • 2
    `requestAnimFrame` is most likely just an alias for `requestAnimationFrame`. In other words: if you called `loop` once, requestAnimFrame will ensure another call to `loop` will be made, regardless of the value of `game`. The current answer is wrong. – Bart Mar 21 '14 at 02:39
7

You need to make sure requestAnimFrame stops being called once game == 1. A break statement only exits a traditional loop (e.g. while()).

function loop() {
    if (isPlaying) {
        jet1.draw();
        drawAllEnemies();
        if (game != 1) {
            requestAnimFrame(loop);
        }
    }
}

Or alternatively you could simply skip the second if condition and change the first condition to if (isPlaying && game !== 1). You would have to make a variable called game and give it a value of 0. Add 1 to it every game.

Community
  • 1
  • 1
Bart
  • 26,399
  • 1
  • 23
  • 24
0

I have a function next() which will maybe inspire you.

function queue(target) {
        var array = Array.prototype;

        var queueing = [];

        target.queue = queue;
        target.queued = queued;

        return target;

        function queued(action) {
            return function () {
                var self = this;
                var args = arguments;

                queue(function (next) {
                    action.apply(self, array.concat.apply(next, args));
                });
            };
        }

        function queue(action) {
            if (!action) {
                return;
            }

            queueing.push(action);

            if (queueing.length === 1) {
                next();
            }
        }

        function next() {
            queueing[0](function (err) {
                if (err) {
                    throw err;
                }

                queueing = queueing.slice(1);

                if (queueing.length) {
                    next();
                }
            });
        }
    }
Dale K
  • 25,246
  • 15
  • 42
  • 71
0

you can do that things also,

const re =  winState.map(elements => {
    let a = gameState[elements[0]];
    let b = gameState[elements[1]];
    let c = gameState[elements[2]];

if you want to quit in this loop simply you

return the whole function

if (a === b && b === c) {
         return re;
    }
}

when this condition is true you quit this loop

Toto
  • 89,455
  • 62
  • 89
  • 125
DSDmark
  • 1,045
  • 5
  • 11
  • 25
0

you cannot use break statement in forEach loop, you can replace forEach with for loop

Muhammad Awais
  • 1,608
  • 1
  • 21
  • 21