Results 1 to 3 of 3

Thread: using "for" looping

  1. #1

    Thread Starter
    Fanatic Member merhaba's Avatar
    Join Date
    Sep 2002
    Location
    Istanbul,Bartin-Gallipoli(Gelibolu-Canakkale)
    Posts
    601

    using "for" looping

    hi there, the following scripts is for moving an object..
    and now I want to make it loop 4 times...how can add a"for looping" to that script
    -------this did not work---------
    <script language="javascript">
    for(var i=1;i<3;i++)
    {
    moveit()
    }
    </script>
    -------------------
    ---------------

    <html>
    <head>
    <style>
    #piccy{color:green;background-color:yellow;width:80;height:80;}
    body{background-color:black;color:yellow;}
    </style>
    <script>
    var delay=10,ver=0, hor=0
    function moveit()
    {
    if(hor<500)
    {
    piccy.style.left= hor;
    hor=hor+5;
    setTimeout('moveit()',delay);
    }

    if(hor==500){moveitd()}
    }
    function moveitd()
    {
    if(ver<330)
    {
    piccy.style.top= ver;
    ver=ver+5;
    setTimeout('moveitd()',(delay*10));
    }
    if(ver==330){moveitl()}
    }
    function moveitl()
    {
    if(hor>10)
    {
    piccy.style.left=hor;
    hor=hor-5;
    setTimeout('moveitl()',(delay+10));
    }
    if(hor==10){moveity()}
    }
    function moveity()
    {
    if(ver>0)
    {
    piccy.style.top=ver;
    ver=ver-5;
    setTimeout('moveity()',(delay+10));
    }
    }

    </script>
    </head>
    <body>
    <center>
    <form>
    <input type="button" value="start" onClick="moveit()">
    <br>
    </form>
    <div id="piccy" style="position: absolute; top: 0px; left: 0px; width: 60px; height:60px;
    visibility: visible">
    <img src="cilek.gif" height="55" width="55">
    </div>

    <script language="javascript">
    for(var i=1;i<3;i++)
    {
    moveit()
    }
    </script>
    </body>
    </html>
    ----------------------


    thanks indeed

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    Your for loop only executes when i = 1 and 2.

    Change for(var i=1;i<3;i++) to for(var i=1;i<=4;i++)

  3. #3
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by ccoder
    Your for loop only executes when i = 1 and 2.

    Change for(var i=1;i<3;i++) to for(var i=1;i<=4;i++)
    I believe this would be faster:

    Code:
    for(var i = 0; i < 4; i++)
    Than using <=.

    At least in C++, it is. But I'm sure speed isn't an issue with this. It's most likely unnoticable.

    But my teacher always heckles us with speed issues since he works on coding airplane response units, and you want them to be as fast as can be.

    Just thought I'd buzz in.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width