Javascript 永无止境的循环?

Javascript 永无止境的循环?,javascript,php,Javascript,Php,我试图在页面上的任意位置显示3个提交按钮。我设法做到了这一点。我现在创建了三个函数,它们为这些按钮生成坐标,并检查它们是否与其他按钮重叠过多 在这些函数的某个地方,我想我正在创建一个无限循环-但我不明白为什么会是这样 这是代码。函数在标题中定义和调用 <?php session_start(); ?> <html> <head> <style> input[type='submit']{

我试图在页面上的任意位置显示3个提交按钮。我设法做到了这一点。我现在创建了三个函数,它们为这些按钮生成坐标,并检查它们是否与其他按钮重叠过多

在这些函数的某个地方,我想我正在创建一个无限循环-但我不明白为什么会是这样

这是代码。函数在标题中定义和调用

<?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." your score so far=".$score;
        echo "<br>";
        ?>

        <?php echo"Write the 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>

输入[type='submit']{
位置:绝对位置;
宽度:300;
身高:50;
颜色:白色;
背景:红色;
}

> > > 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=+“px”; btn.style.left=+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=+“px”; btn2.style.left=+“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=+“px”; btn3.style.left=+“px”;
检查重叠时,应使用abs()。以下条件将始终通过,因为$x2-$x1或$x1-$x2中的一个将为负值(因此小于30)


if($x2-$x1在
secondxy
中,
$x1
始终是未定义的。在thirdxy中也是如此also@chris85所以,如果我声明$x1,$y1等…最初,在函数外部,然后在函数内部声明它们为全局的…应该可以工作吗?此时函数中的值可以访问。我不确定它是否会工作正如您所期望的那样。在调试时打开错误报告也会很好。它将始终通过,因为$x1始终未定义。
if ($x2-$x1<30 ||$x1-$x2<30)
if (abs($x2-$x1) < 30){