Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Location
    UK
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problem with setTimeout...

    I am trying to create a script that will move an image off the side of the screen gradually (but still pretty quickly). I currently have the code:
    Code:
    <script>
    var distance = 50;
    
    function move() {
      If (distance < "500") {
      zelda.style.left = distance;
      distance=distance+50;
      setTimeout("move();", 100);
      }
    }
    </script>
    
    <center>
    <img style="position: 'relative'; left: -100;" src="zelda.jpg" name="zelda" onMouseOver="move();">
    </center>
    For some reason whenever I run this code it stops when trying to call the move() function with an error of "object expected".
    if i remove all the lines of code in the function and add them all back in individually it works up to the point where I add the setTimeout() command so I'm assuming it's a setTimeout() problem.

    Thanks for your help.
    Last edited by MoRiA; 01-19-2003 at 05:40 PM.

  • #2
    New Coder
    Join Date
    Sep 2002
    Location
    Moncton, N.B., Canada
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this:

    Code:
    
    var distance = 50;
    
    function move() {
      if (distance < 500) 
      {	
      document.getElementById('zelda').style.left = distance; 
      distance=distance+50;
      setTimeout("move()", 100); 
      }
    }
    
    
    ----------------------------------------------------------
    
    <!-- html -->
    <img style="position: relative; left: -100;" src="zelda.jpg" id="zelda" onMouseOver="move();">
    You had a few things wrong. 1) your if statement had "If" instead of "if"... js is case sensitive; 2) since you were referencing the image by its name, your syntax for the css wasn't working, I changed it to an id; 3) your call to setTimeout doesn't take the semi-colon after the function call, I took it out.

    Hope this helps.

    - Terry

  • #3
    New to the CF scene
    Join Date
    Aug 2002
    Location
    UK
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, it works fine now


  •  

    Posting Permissions

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