1

I'm trying to move a div on a semi circular path. I have tried the parametric equation and it works.

I don't know why using for loops makes my program to stop working. When I try to move it using a for loop, The browser stops working and the memory usage skyrockets.

I don't know what is the problem. Please Advise.

<html>
<head>
    <title>movediv</title>
    <style type="text/css">
        .bodycss {
            background-color: aqua ;
        }
        .posmove{
            position: absolute;
            background-color: blue;
            width: 20px;
            height: 20px;   
        }
        .buttonpos{
            position: absolute;
            top:250px;
            left:250px;
            background-color: "green";
        }
    </style>
    <script type="text/javascript">
        var i=0,j=0;
        var x = 100;
        function movediv()
        { 
            for(i=250-x;i<251+x;i++)
            {   
                j = 250 + Math.floor(Math.sqrt((x*x) - (i-250)*(i-250)));

                setTimeout(function() 
                { 
                    document.getElementById('point').style.top = j + "px";
                    document.getElementById('point').style.left = i + "px";
                }, 100);

              if(i==250+x)  i=250-x;
            }
        }
    </script>
</head>
<body class="bodycss">
    <div class="posmove" id="point"></div>
    <form>
        <input type="button" id="radius" class="buttonpos" onclick="movediv();"  > 
    </form> 
</body>
</html> 
Blundering Philosopher
  • 6,245
  • 2
  • 43
  • 59
GokulSrinivas
  • 383
  • 2
  • 10
  • you are crashing the browser because you are creating an infinite for loop. you are on the right path using setTimeout, but you need to create a setTimeout that calls a function which executes the for loop once, and set that on a timer. – chiliNUT Mar 15 '14 at 05:33
  • I have tried that as well. The browser ran the script only once and stopped. any ideas? Thanks for the answer though. :) – GokulSrinivas Mar 16 '14 at 05:36
  • post a fiddle of what you've tried – chiliNUT Mar 16 '14 at 05:55

0 Answers0