我希望我的程序成功完成的任務是從命令提示符變量ask
中獲取一個值並檢查它是否正確。但是如果玩家沒有及時回答,設定的超時功能會執行,因此會減去壽命。爲什麼沒有設置超時功能激活win語句?
我的問題是當我輸入提示ask
超時功能根本沒有設置。
另外,我把所有東西都放在for循環中,所以當玩家確實得到正確答案時,它會再次運行並減去時間,因此解決問題的時間越來越少。我的代碼如下。
var win = null;
var product = "123456";
time = 3000;
for(var i = 0; i < 1; i++) {
confirm("Round will start after you close this confirm window.");
setTimeout(function() {
var ask = prompt("Enter code below: (QUICK) " + product + " (Answer for development purposes)");
if(ask === product){
win = true;
console.log("Hurrah!");
} else {
win = false;
console.log("Arr. Well get em next time.");
}
}, 1);
//Why is this not activating???
setTimeout(function() {
if (win === null || false) {
lives = lives - 1;
win = false;
console.log("Oh No! You ran out of time. Lives left: " + lives);
}
}, time);
time = time - 100;
}
什麼是「生命」?另外,'win === null ||假'應該是'win === null || win === false'或者它永遠不會執行 – Li357
'prompt()'不支持超時,並且因爲它是一個阻塞函數,所以在完成之前沒有別的東西可以運行。 – jfriend00
你想爲此只有一個JavaScript的解決方案。無論@ jfriend00評論過的是你的問題的原因。而jQuery可以輕鬆解決你的問題。 – Manish