尝试在嵌套的if \ else语句中调用break

时间:2013-09-14 18:48:07

标签: javascript if-statement break

这是我的代码的前面,我喜欢在" else"之后嵌入一个break语句。如果答案超过18

setTimeout(function(){
confirm("I am ready to play!");
age= prompt("What's your age?");
    if(age<18){
        alert('You are allow to play!');
    }
    else{
    alert("No!"); 
    };

2 个答案:

答案 0 :(得分:1)

breakcontinue只能在循环中使用。如果要停止执行函数代码,只需添加return;

setTimeout(function(){
if (!confirm("I am ready to play!")) return; // I suppose that's you really want.
age= prompt("What's your age?");
    if(age<18){
        alert('You are allow to play!');
    }
    else{
        alert("No!");
        return; // Stop the function.
    }
}, timer);

答案 1 :(得分:0)

您需要返回退出函数而不是休息。如果需要,请检查是否为假,或者只是在那里写回来

setTimeout(function(){
confirm("I am ready to play!");
age= prompt("What's your age?");
    if(age<18){
        alert('You are allow to play!');
    }
    else{
return false;
    };