这是我的代码的前面,我喜欢在" 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!");
};
答案 0 :(得分:1)
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);
答案 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;
};