PDA

View Full Version : setTimeout


Endeavor88
03-16-2005, 11:38 PM
i need to use the setTimeout method.

how can i call the function:
move(div,x,y);

?

this does not seem to work:
setTimeout("move(div,x,y)", 1000);

IKLOP
03-17-2005, 12:09 AM
try this:

var str = "move(" + div + "," + x + "," + y + ")";
setTimeout(str, 1000);

Endeavor88
03-17-2005, 12:21 AM
it does not totally work,
i get an error that says on line 1, char 1, 'object' is undefined.
when i tried to troubleshoot the string by displaying it, it shows "move([object], 234, 23)". that seems right to me...

what's wrong?

IKLOP
03-17-2005, 12:55 AM
didn't realize div was an object, try this:

setTimeout( function(){ move(div,x,y) }, 1000 );

Endeavor88
03-17-2005, 11:51 AM
thanks, that works.
i have another question.

i made a function called "move" and made that function call it self by setTimeout for 10 times. and as you saw above, the function receives a div object.
i have two problems.

1) every time i call the function, i increment div.lapse by 1. once that reaches 10 i want to stop calling the move function and call another function. but javascript seems to read the whole script through without waiting for the end of the setTimeout function...
my move function moves a div to a certain location.

2) if i have 2 divs and i want to move them both, then how do i use the function move simultaneously?

IKLOP
03-17-2005, 12:31 PM
1. You can just put an if statement in your function that checks to see if div.lapse >= 10, if it is then call the other function, else setTimout:functino main( div, x, y ) {
//do your stuff here

//check div.lapse
if( div.lapse>10 ) {
anotherFunctino();
} else {
setTimeout( function(){ move(div,x,y) }, 1000 );
}
}

2. You could just call main twice:main(div1, x1, y1); main(div2, x2, y2);the functions won't be called at exactly the same time, but they will be done so fast that it will look as though they are.

Endeavor88
03-17-2005, 12:47 PM
yeah, i figured the 1st part out by myself earlier.

i made a button to push that calls a function to create a div and then that function calls another function to start the movement of the div. so i can keep pressing the button, creating an unlimited amount of divs. but whenever i create a new div, the previous div halts its movement.

IKLOP
03-17-2005, 01:13 PM
hmmm, not sure why that would be happening, perhaps you could give a link to the page or post the code?

Endeavor88
03-17-2005, 01:51 PM
hmm...perhaps i'm mistaken.
i have two pages, so maybe i got confused.
http://endeavor.aspfreeserver.com/dhtml/squares/

Endeavor88
03-17-2005, 03:06 PM
it seems like it works.

i have an interesting question now, but it probably has to do with IE i'm thinking...

after i made over 100 squares, and they all stopped, i selected them all (ctrl a) and copied and pasted them into notepad. the squares apparently are in random places, but when i look at notepad, i see all the numbers in order, beginning with 1 and ending with the last square.

is there a reason for this?

Endeavor88
03-17-2005, 03:45 PM
http://endeavor.aspfreeserver.com/dhtml/squares/

how can i make this page work in firefox?

IKLOP
03-17-2005, 03:48 PM
cool effect, one thing to point out though: it doesn't work in firefox. my guess is that it should be window.innerHeight and window.innerWidth instead of self.innerWidth and Height.

as far as your question, it seems to have to do with the fact that the text is absolutely positioned, and when this is the case, it seems browsers get the text in order that they are coded instead of displayed, for example:<div style='position:absolute; top:400px; left:10px;'>Bottom</div>
<div style='position:absolute; top:200px; left:10px;'>Top</div>will display in a browser as:Top
Bottombut when you copy and paste it is displayed asBottom
Top
which is the order it was in the code, and since you create the box's in order, they are displayed in order. I doubt there is any way to get around it.

Endeavor88
03-17-2005, 05:22 PM
the reason the div does not appear in firefox (i just discovered) is that my z-index for all squares is -1. I made it -1 so that it would go behind all other document "things" (what word :confused:). but mozilla does not seem to allow -1.

so how do i make it work in both IE and firefox?

Endeavor88
03-17-2005, 06:22 PM
i got another question, but it's kind of hard to visualize and hard to reproduce.

the square moves seemingly in a line, which it should be doing.

when a square is going somewhere, at the very end, before it gets to its destination, it seems to stop moving horizontally and it moves up or down a few pixels to its destination (sometimes it looks like it moves 15 pixels, sometimes only 2 pixels). when the squares move, it doesn't always happen...

i can't seem to figure out what is wrong.

Endeavor88
03-18-2005, 12:23 AM
is someone going to respond?

senshi
03-18-2005, 04:17 AM
Originally posted by Endeavor88
i need to use the setTimeout method.

how can i call the function:
move(div,x,y);

?

this does not seem to work:
setTimeout("move(div,x,y)", 1000);

you refer to the object WITHOUT the Quotes

setTimeout( move(div,x,y), 1000);

the quotes only passes a string, you need to pass the function to the event call that you require running.

IF this is a regular call at a regular time interval, set time out should be replaced by the set interval timer where it only needs setting once until cancled by the clear event call.

IKLOP
03-18-2005, 07:35 PM
the reason the div does not appear in firefox (i just discovered) is that my z-index for all squares is -1. I made it -1 so that it would go behind all other document "things" (what word :confused. but mozilla does not seem to allow -1.

so how do i make it work in both IE and firefox?
what other document things does it need to go behind? I guess you could try setting the z-index of the body to something like 2, then set the squares to 1, not sure that would work.



i got another question, but it's kind of hard to visualize and hard to reproduce.

the square moves seemingly in a line, which it should be doing.

when a square is going somewhere, at the very end, before it gets to its destination, it seems to stop moving horizontally and it moves up or down a few pixels to its destination (sometimes it looks like it moves 15 pixels, sometimes only 2 pixels). when the squares move, it doesn't always happen...

i can't seem to figure out what is wrong. i can't see what's happening since I don't have IE and I'm too lazy to reboot into windows, so I can't really help you with that, though it might have something to do with rounding. You might want to only round the output and not the actual values. that's just a total guess though.


you refer to the object WITHOUT the Quotes

setTimeout( move(div,x,y), 1000); if you don't use quotes then the function will be called immediately and in 1000ms, which you don't usually want. You need to use quotes to make it not call immediately, but since div is an object, that won't work, hence the function work around i mentioned.