好的,除了写一个超时之外我几乎完成了所有事情,如果他们在10秒钟内没有按下Guess按钮,他们会询问用户是否要停止游戏。如果用户选择“确定”,请关闭Web浏览器窗口。我无法弄清楚这是我到目前为止所做的。
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<HTML>
<HEAD>
<TITLE>Guess the number</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
hiddenNumber = Math.round((Math.random() * 100))%100 + 1;
function checkGuess(obj) {
var status;
var guess=obj.value;
if (guess > hiddenNumber) {
status=(guess+" is Too high!\nTry again");
}
else if (guess < hiddenNumber) {
status=(guess+" is Too low!\nTry again");
}
else if (guess == hiddenNumber){
status=("Congratulations! You guessed the number!\n"+guess);
}
alert(status);
obj.value='';
}
function continueGame(){
var quit = confirm("Do you want to stop playing?");
if (quit == true)
window.close();
}
// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>
</HEAD>
<BODY>
<H1>Guessing Game</H1>
Guess a number between 1 - 100:
<FORM NAME="guessForm">
<INPUT TYPE="text" NAME="guessField">
<INPUT TYPE="button" VALUE=" Guess "
onClick="checkGuess(document.guessForm.guessField);">
</FORM>
</BODY>
</HTML>
答案 0 :(得分:1)
timeout=setTimeout(checkExit,10*1000);
if (/*pressed on guess*/)
{
clearTimeout(timeout);
timeout=setTimeout(checkExit,10*1000);
}
function checkExit()
{
if (confirm("Exit?")) window.close();
else //reset timeout here again
}
答案 1 :(得分:0)
如果在代码中添加它,十秒后将继续运行continueGame()函数:
var t = setTimeout("continueGame()",10000);