这是我的代码的前面,如果答案超过 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!");
};
这是我的代码的前面,如果答案超过 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!");
};
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);
您需要返回退出函数而不是中断。之后如果需要检查是否为 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;
};