...

View Full Version : Problem with setTimeout...



MoRiA
01-19-2003, 04:38 PM
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:


<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.

Terry
01-19-2003, 05:14 PM
Try this:





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

MoRiA
01-19-2003, 06:33 PM
thanks, it works fine now :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum