简体   繁体   English

为什么在其他提示之前不运行

[英]Why doesn't this if/else run before the other prompts

var password = prompt('Please enter your password.');
if (password!=="cat" && password!=="cow") {
console.log(password);
location.assign("http://www.google.com")
}


//The rest of prompts
var age = prompt("What is your age?");
var name = prompt('What is your name?');
var homeTown = prompt('Where are you from?');
var favoritDog = prompt("What's your favorite dog?");

So that's my code, (I'm a beginner and just messing around with concepts), if the incorrect password is put in, shouldn't it redirect me to google right away? 这就是我的代码((我是一个初学者,只是在弄乱概念),如果输入了错误的密码,它是否应该立即将我重定向到Google? Because when I run that code, it gives me all the prompts first before the redirect. 因为当我运行该代码时,它将在重定向之前先给我所有提示。 Any help is appreciated, thank you. 任何帮助表示赞赏,谢谢。

Because doing location.assign will not stop the execution of JavaScript, it will keep going. 因为执行location.assign不会停止JavaScript的执行,所以它将继续进行。 It will do the location assignment after the sequential JavaScript finishes. 顺序JavaScript完成后,它将进行位置分配。 Since prompt blocks the current execution, for it to finish it will need to go through the prompts. 由于prompt阻止当前执行,因此要完成它,就需要仔细阅读提示。 To prevent it simply add your prompts to an else : 为了防止这种情况,只需将提示添加到else

var password = prompt('Please enter your password.');
if (password!=="cat" && password!=="cow") {
    console.log(password);
    location.assign("http://www.google.com")
} else {
    //The rest of prompts
    var age = prompt("What is your age?");
    var name = prompt('What is your name?');
    var homeTown = prompt('Where are you from?');
    var favoritDog = prompt("What's your favorite dog?");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM