Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Javascript setInterval() method problem
Closed Thread
Old 04-25-2009, 09:40 PM   #1 (permalink)
oldskool's Avatar
 
Electrical Systems Design

Join Date: Jun 2008

Location: Maine, USA

Posts: 1,646

oldskool has a spectacular aura aboutoldskool has a spectacular aura about

Default Javascript setInterval() method problem

*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.
oldskool is offline  
Old 04-26-2009, 09:48 AM   #2 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: Javascript setInterval() method problem

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);
       }
       
}

__________________

Need website help? PM me!
CrazeD is offline  
Old 04-26-2009, 06:57 PM   #3 (permalink)
oldskool's Avatar
 
Electrical Systems Design

Join Date: Jun 2008

Location: Maine, USA

Posts: 1,646

oldskool has a spectacular aura aboutoldskool has a spectacular aura about

Default Re: Javascript setInterval() method problem

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 !
oldskool is offline  
Old 04-28-2009, 12:47 AM   #4 (permalink)
oldskool's Avatar
 
Electrical Systems Design

Join Date: Jun 2008

Location: Maine, USA

Posts: 1,646

oldskool has a spectacular aura aboutoldskool has a spectacular aura about

Default Re: Javascript setInterval() method problem

Quote:
Originally Posted by oldskool View Post
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 !
EDIT: I tried it out, Craze, and it worked fine.
oldskool is offline  
Old 04-28-2009, 11:15 AM   #5 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: Javascript setInterval() method problem

Quote:
Originally Posted by oldskool View Post
EDIT: I tried it out, Craze, and it worked fine.
Good to hear.
__________________

Need website help? PM me!
CrazeD is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript function problem oldskool Programming Discussions 6 03-24-2009 09:52 PM
The A-Z of Programming Languages: JavaScript Osiris Programming Discussions 0 08-04-2008 09:35 AM
Problem installing software in windows xp pro imagesmith Windows Operating Systems and Software 3 02-20-2008 02:46 PM
Serious computer problem. HeeRoMaKi Hardware Troubleshooting 71 07-28-2007 11:42 PM
Debugging Javascript Application Using Date Method Cunjo Programming Discussions 1 04-17-2007 11:07 PM