javascript - Never-ending loop? -


i trying display 3 submit buttons on page @ random positions. have managed this. have created 3 functions generate coordinates these buttons , check not overlap other buttons.

somewhere in these functions think creating infinite loop - can't see why case...

here code. functions defined , called in header.

<?php session_start(); ?> <html>     <head>         <style>             input[type='submit']{                 position: absolute;                 width: 300;                 height: 50;                 color: white;                 background: red;             }         </style>         <?php           $first=array(2,4,8,10);         $second=array(2,4,8,10);          $b=rand(0,3);         $c=rand(0,3);          $f=$first[$b];         $s=$second[$c];          $d=$f*$s;         $score=$_session["score"];         $name=$_session["name"];          function firstxy(){          $x1=rand(0,500);         $y1=rand(0,500);         }          function secondxy(){             $x2=rand(0,500);             $y2=rand(0,500);             if ($x2-$x1<30 ||$x1-$x2<30){             secondxy();         }             if ($y2-$y1<30 ||$y1-$y2<30){             secondxy();         }}          function thirdxy(){             $x3=rand(0,500);             $y3=rand(0,500);             if ($x3-$x2<30 ||$x3-$x1<30||$x2-$x3<30||$x1-$x3<30){             thirdxy();         }             if ($y3-$y2<30 ||$y3-$y1<30||$y2-$y3<30||$y1-$y3<30){             thirdxy();         }}          firstxy();          secondxy();          thirdxy();         ?>         </head>     <body>          <?php          echo $name." score far=".$score;         echo "<br>";         ?>          <?php echo"write answer";?>         <br>         <?php echo $f."x".$s."=";?>     <form method= "post" action="submit.php">         <input type="number" name=a value="0">         <input type="hidden" name=b value=<?php echo $f;?>>         <input type="hidden" name=c value=<?php echo $s;?>>         <input type="submit" name=submit value=<?php echo $d;?>>          <input type="submit" id="btn" value=<?php echo $d;?>>     <input type="submit" id="btn2" value=<?php echo " wrong answer";?>>     <input type="submit" id="btn3" value=<?php echo " wrong answer";?>>     </form>     <script>             var btn = document.getelementbyid("btn"); //btn.style.top = math.floor((math.random() * 230) + 1) + "px"; //btn.style.left = math.floor((math.random() * 200) + 1) + "px"; btn.style.top = <?php echo $x1;?>+ "px"; btn.style.left = <?php echo $y1;?> + "px";  var btn2 = document.getelementbyid("btn2"); //btn2.style.top = math.floor((math.random() * 230) + 1) + "px"; //btn2.style.left = math.floor((math.random() * 200) + 1) + "px"; btn2.style.top = <?php echo $x2;?>+ "px"; btn2.style.left = <?php echo $y2;?> + "px";  var btn3 = document.getelementbyid("btn3"); //btn3.style.top = math.floor((math.random() * 230) + 1) + "px"; //btn3.style.left = math.floor((math.random() * 200) + 1) + "px"; btn3.style.top = <?php echo $x3;?>+ "px"; btn3.style.left = <?php echo $y3;?> + "px";         </script>  </body></html> 

when checking overlap, should use abs(). following condition pass, because 1 of $x2-$x1 or $x1-$x2 negative (therefore less 30).

if ($x2-$x1<30 ||$x1-$x2<30) 

try doing like

if (abs($x2-$x1) < 30){ 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -