我希望我的程序成功完成的任务是从命令提示符变量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