4 Replies - 851 Views - Last Post: 23 September 2013 - 03:31 PM

#1 EdNolan  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 412
  • Joined: 22-September 13

Random number page refresh

Posted 22 September 2013 - 07:48 PM

Hi, I've been working on a program but I can't get past this problem I am having. The way the program works is, It randomly picks and displays 4 number. There is a timer that counts down to zero. When the timer reaches zero I have another random number generator pick another number. all this works good. The only thing is that I wanted the 5th random number to be added next to the first four that got picked. The problem is when the new random number generator picks the 5th number it clears the screen and displays it all by itself. Is there a way to get this 5th digit to show up next to the first four?

Here's the code ...

<html>
<head>
</head>
<body>

<script type="text/javascript">
<!--
//TIMER COUNT DOWN STARTS HERE
//-->
</script>
<br>
<br>
<form name="counter"><input type="text" size="8" 
name="d2"></form> 

<script> 
<!-- 
// 
var milisec=0 
var seconds=15
document.counter.d2.value='10' 

function display(){ 
if (milisec<=0){ 
milisec=9 
seconds-=1 
} 
if (seconds<=-1){ 
milisec=0 
seconds+=1 
//------DRAW 1 MORE CARD ---------alert("Draw Card " + name);
//--After the initial timer this will pick the fifth, but I don't want it to clear
//--the screen. How do I get this to pick the fifth, but add it to the end of the
//--first four that where picked?
var rand = (Math.ceil(Math.random()*4));

switch(rand){
    case 1:
       document.write("1  ");
       break;
    case 2:
       document.write("2  ");
       break;
    case 3:
       document.write("3  ");
       break;
    case 4:
       document.write("4  ");
       break;
    default:
       document.write("No Match !!!  ");       
}
//------END OF 5 CARDS BEING PICKED
} 
else 
milisec-=1 
document.counter.d2.value=seconds+"."+milisec 
setTimeout("display()",100) 
} 
display() 
//THIS IS THE END OF THE TIMER COUNT DOWN

 document.write("<br />");
 document.write("<br />");
 document.write("<br />");
 document.write("<br />");

//THIS PICKS THE FIRST FOUR CARDS
// For(       how many time to run       ) { what to run }
//    (         three statements         )
//    ( varx=0;        x<9;        x++  )
//    (start point   end point    1count )


for (x=0;x<4;x++){

var rand = (Math.ceil(Math.random()*10));

switch(rand){
    case 1:
       document.write("1  ");
       break;
    case 2:
       document.write("2  ");
       break;
    case 3:
       document.write("3  ");
       break;
    case 4:
       document.write("4  ");
       break;
    case 5:
       document.write("5  ");
       break;
    case 6:
       document.write("6  ");
       break;
    case 7:
       document.write("7  ");
       break;
    case 8:
       document.write("8  ");
       break;
    case 9:
       document.write("9  ");
       break;
    case 10:
       document.write("10  ");
       break;
    default:
       document.write("No Match !!!  ");       
}
}
//THIS ENDS THE FIRST FOUR CARD PICKS


--> 
</script>


</body>
</html>

This post has been edited by modi123_1: 23 September 2013 - 08:28 AM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Random number page refresh

#2 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 138
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Random number page refresh

Posted 22 September 2013 - 07:55 PM

Maybe try here,

http://www.dreaminco.../90-javascript/
Was This Post Helpful? 0
  • +
  • -

#3 laytonsdad  Icon User is online

  • Cheese and Sprinkles
  • member icon

Reputation: 451
  • View blog
  • Posts: 1,947
  • Joined: 30-April 10

Re: Random number page refresh

Posted 23 September 2013 - 10:27 AM

You need to look into using getElementById() because using document.write() will replace what is on the page most of the time.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,589
  • Joined: 12-December 12

Re: Random number page refresh

Posted 23 September 2013 - 03:25 PM

To get your Javascript, and HTML, more up-to-date (and correct):

  • Every page requires a DOCTYPE declaration and a title
  • Javascript doesn't need, or use, the html-comments <!-- -->
  • Forms no longer have a name attribute (but should have an action attribute)
  • Javascript statements are semi-colon terminated (even though it can work without them)
  • document.write should be avoided (as laytonsdad has indicated)

Instead of document.write add your scripts at the bottom of the page (before the closing body-tag) so that the page has fully loaded, and perhaps use innerHTML to modify the content of page-elements.

If you are reading a tutorial you should find one that is more up-to-date.

This post has been edited by andrewsw: 23 September 2013 - 03:26 PM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,589
  • Joined: 12-December 12

Re: Random number page refresh

Posted 23 September 2013 - 03:31 PM

BTW Your switch-statements are largely redundant because you can just use an if-statement and construct a string using
rand + "  "

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1