Javascript setInterval() method problem

Status
Not open for further replies.

oldskool

Oldie but a goodie
Messages
1,760
Location
Maine, USA
*NOTE:
This is a homework problem, so I don't need specifics so much as just a little hint. Using pseudocode might be enough for me to figure it out on my own without giving away the answer directly. Thanks in advance by the way if anyone can help...

What I am trying to do is after the user guesses the correct number, set the setInterval() method so that in 10000 milliseconds the user will get the confirmation box I have set up asking whether they want to quit or not. To click OK will window.close(); To click CANCEL will allow the user to keep playing and reset the game.

I have it all working except for the interval method and reset aspects of it.

Here's the code thus far:

Code:
<html>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Math Guess HTML</title>

<meta http-equiv="content-type" content="text/html;
     charset=iso-8859-1" />

<center><h4>Guess a number between 1 and 100 and enter it in the text box<h4></center>


<script type="text/javascript">
/* <![CDATA[ */
//code written by Eric Maddan



var rndm = Math.round(Math.random() * 100);

function confBox()  {
    return window.confirm("Would you like to quit? If so click OK, otherwise click CANCEL to keep playing");   
}


function guessNumber()                {

    var guessed = document.mathguess.number.value;
    
    if (guessed < rndm)   {
       window.alert("Higher !");
    }
    else if(guessed > rndm)   {
       window.alert("Lower !");
    }
    
       if (guessed == rndm) {
         window.alert("You guessed the number !");
         return confBox()
       }
       
}





   

/* ]]> */

</script>

</head>

<body>

<script type="text/javascript">
/* <![CDATA[ */








/* ]]> */

</script>

&nbsp&nbsp&nbsp&nbsp

<center>
<form name="mathguess">

<input type="text" name="number" size="20"><br />
<input type="button" name="guessButton" value="Guess"
  onclick="guessNumber()";>


</form>
</center>

</body>
</html>

Here is the code I wish to add for the setInterval() method:

Code:
setInterval('confBox()',10000)

And to close the window:

Code:
window.close();

I know I have the code right except for getting the confirmation box to show up after 10 seconds (10000 milliseconds as I mentioned) and then the user has the choice of clicking
click OK to quit and close the window, or click CANCEL to keep playing the game.
 
Well since you didn't put the setInterval in your code anywhere, I don't really know where you want it...

However I will say that you should be using setTimeout() instead. setTimeout works exactly the same as setInterval, except it only calls the function once, where as setInterval calls it every X milliseconds.

You said you want to delay the confirmation box from appearing, so I guess this is what you want:

Code:
function guessNumber()                {

    var guessed = document.mathguess.number.value;
    
    if (guessed < rndm)   {
       window.alert("Higher !");
    }
    else if(guessed > rndm)   {
       window.alert("Lower !");
    }
    
       if (guessed == rndm) {
         window.alert("You guessed the number !");
         return setTimeout("confBox()",10000);
       }
       
}
 
Yes, I didn't know where to put it to make it work, which is why I left the scraps at the end lol !

Thanks, looks good ! Will try it out. Much appreciated CrazeD !
 
Status
Not open for further replies.
Back
Top Bottom