非法使用休息声明; JavaScript的 [英] illegal use of break statement; javascript

查看:79
本文介绍了非法使用休息声明; JavaScript的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当这个变量变成一定数量时,我希望循环停止,但我不断收到错误,Uncaught SyntaxError:Illegal break statement。

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;

        }

    }
} 


推荐答案

中断是要打破一个循环,比如,你在这里没有切换等,你需要使用 return 来打破当前函数的执行流程并返回调用者。

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
        }
    }
}

注意:这不包括if条件背后的逻辑或何时从方法返回,因为我们需要有关于 drawAllEnemies的更多上下文 requestAnimFrame 方法以及游戏值的更新方式

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

这篇关于非法使用休息声明; JavaScript的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆