簡體   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?");

這就是我的代碼((我是一個初學者,只是在弄亂概念),如果輸入了錯誤的密碼,它是否應該立即將我重定向到Google? 因為當我運行該代碼時,它將在重定向之前先給我所有提示。 任何幫助表示贊賞,謝謝。

因為執行location.assign不會停止JavaScript的執行,所以它將繼續進行。 順序JavaScript完成后,它將進行位置分配。 由於prompt阻止當前執行,因此要完成它,就需要仔細閱讀提示。 為了防止這種情況,只需將提示添加到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