0

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

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!"); 
    };
4

2 回答 2

2

break或者continue只能在循环中使用。如果你想停止执行一个函数的代码,只需添加一个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);
于 2013-09-14T18:59:36.233 回答
0

您需要返回退出函数而不是中断。之后如果需要检查是否为 false,或者只是在那边写 return

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;
    };
于 2013-09-14T18:54:07.407 回答