473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

setTimeout causes return of 32 million instead of x,y coordinate

I'm trying to move pictures around on the screen, with a slight delay
between the first picture and second picture (following onmousemove).
When I add the setTimeout function I must be doing it wrong because
the function that calculates the new coordinates for the picture
returns 32 million and change instead of the coordinate. This causes
the picture to not be able to appear on the display, obviously.

Here's the calling code (snipped for readability) that works without
setTimeout:

*********************************
function moveDucks(xPos, yPos) {

baby1.left = newDuckPos(yPos - 30, xPos - 30)
baby1.top = newDuckPos(xPos + 30, yPos + 30)
}
function newDuckPos(currentPos, duckPos) {

newPos = Math.min(Math.max(currentPos, duckPos+3), duckPos+17)

return(document.getElementById) ? newPos + "px" : newPos

}
****************************************

The above returns the correct "newPos" and everything is hunky dory.
Then I
try to use setTimeout:
****************************************
function moveDucks(xPos, yPos) {

baby1.left = setTimeout("newDuckPos("+yPos+" - 30, "+xPos+" -
30)",500);
clearTimeout;

baby1.top = setTimeout("newDuckPos("+xPos+" + 30, "+yPos+" +
30)",500);
clearTimeout;
}
**************************************

The above causes the newDuckPos to return as 32 million+ instead of
normal corrdinates. I am far too new to Javascript to have a clue. I
did follow the i/o enough to see that the the coordinates have valid
values when they reach newDuckPos (I output them to display to make
sure). Then newDuckPos calculates them and before it sends them back
I intercept them again. Again, the values look good.

Then newDuckPos returns them and they get munged.

Can anybody steer me in the right direction to figure this out?
Thanks.
Jul 20 '05 #1
10 3026

"Jupiter49" <ju*******@msn.com> schreef in bericht
news:c7**************************@posting.google.c om...
I'm trying to move pictures around on the screen, with a slight delay
between the first picture and second picture (following onmousemove).
When I add the setTimeout function I must be doing it wrong because
the function that calculates the new coordinates for the picture
returns 32 million and change instead of the coordinate. This causes
the picture to not be able to appear on the display, obviously.

When saying something like:
baby1.top = setTimeout(....);


baby1.top will hold a reference to the setTimeout call instead the new
offset.

Try the following (look at the comments):

function moveDucks(xPos, yPos) {
// The statement +varname ensures that varname isn't treated as a string
// so, when xPos eq 1, xPos + 30 won't become "130"
xPosIncr = +xPos + 30;
yPosIncr = +yPos + 30;
xPosDecr = +xPos - 30;
yPosDecr = +yPos - 30;

setTimeout("baby1.left = newDuckPos("+yPosDecr+", "+xPosDecr+")",500);
setTimeout("baby1.top = newDuckPos("+xPosIncr+", "+yPosIncr+")",500);
}

JW

Jul 20 '05 #2
Janwillem Borleffs wrote:


When saying something like:

baby1.top = setTimeout(....);

baby1.top will hold

the timer id value returned by setTimeout, jah?

dag,
Dom

Jul 20 '05 #3

"Dom Leonard" <do*************@senet.andthis.com.au> schreef in bericht
news:fR*****************@nnrp1.ozemail.com.au...
baby1.top will hold

the timer id value returned by setTimeout, jah?


Which is a reference...

JW

Jul 20 '05 #4
"Janwillem Borleffs" <jw*@jwbfoto.demon.nl> writes:
"Dom Leonard" <do*************@senet.andthis.com.au> schreef in bericht
news:fR*****************@nnrp1.ozemail.com.au...
the timer id value returned by setTimeout, jah?

Which is a reference...


That is a confuzing terminology. In my browsers, the value of
setTimeout("",100) is the number 1 (the first time, next time it is
2). It doesn't refer to anything, in the sense that you can follow
the reference to the target. it is just a number that signifies
something to the internals of the browser.
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

"Lasse Reichstein Nielsen" <lr*@hotpop.com> schreef in bericht
news:is**********@hotpop.com...

That is a confuzing terminology. In my browsers, the value of
setTimeout("",100) is the number 1 (the first time, next time it is
2). It doesn't refer to anything, in the sense that you can follow
the reference to the target. it is just a number that signifies
something to the internals of the browser.


Actually, it does, because it's an id from the timeout process. You can
store this id in a variable and use it as an argument in clearTimeout to
kill the specific timeout process.

This is demonstrated on the following page:

http://www.jwscripts.com/playground/settimeout.php
JW

Jul 20 '05 #6
JW, thanks so much, this really did the trick. Yes!

Question: Since I'm learning this graphical kind of stuff, and since I
see it's really not explained in any depth in the one book I've got, I
wonder if you could steer me in the right direction for more
documentation of the depth that you provided here.

Thanks again!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #7

"Paul LeBlanc" <pa***********@msn.com> schreef in bericht
news:3f***********************@news.frii.net...

Question: Since I'm learning this graphical kind of stuff, and since I
see it's really not explained in any depth in the one book I've got, I
wonder if you could steer me in the right direction for more
documentation of the depth that you provided here.


The best references IMO are from the publisher 'O Reilly.

Example: javascript: The Devinitive Guide. Just visit
http://www.oreilly.com/ and search for JavaScript/DHTML for more info.
Regards,
JW

Jul 20 '05 #8

Lasse, somehow this thread dropped your post from 9/8/03 that described
several issues, one being the setting of the conditional loop inside the
setTimeout statement. You recommended the following:

setTimeout('x+=1; y-=1; moveDucks('+ x +','+ y +')',500);

For some reason this cause a hang as if there is an infinite loop going
on. Here's where the code is placed:
function seedPos(xaxis, yaxis) {

x=xaxis;
y=yaxis;

do {

setTimeout('x+=1; y-=1; moveDucks('+ x +','+ y +')',500);

//x+=1; y-=1;
setTimeout('moveDucks('+ x +','+ y +')',500);

TextBox4.value=(x +","+y)
} while (x < 400);
}
I posted my page at
http://dotdot.sectorlink.org/html/go...cks%20auto.htm if you
would be so kind as to refer to it for the full source. I think all I
need is a way to slow down the above do/while loop.

I may need more in the function called "newDuckPos" too but I am not
sure, and wouldn't know how to encapsulate the return in setTimeout.
Thanks for looking at it. ~Paul

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #9
Paul LeBlanc <pa***********@msn.com> writes:
Lasse, somehow this thread dropped your post from 9/8/03 that described
several issues, one being the setting of the conditional loop inside the
setTimeout statement. You recommended the following:

setTimeout('x+=1; y-=1; moveDucks('+ x +','+ y +')',500);
Did I? Damn. I shouldn't have, because that is wrong.

There were two suggestions:
1) move the updating of x and y into the scheduled code, but keep the
variable *names* in the call to moveDucks (which is what 'x+=1;...' does).
2) Keep the updating of x and y in the code that calls setTimeout, and
use the *values* of x and y in the call (which is what '...'+x+'...' does).
The above line combines the two, which will *not* work.

So, either
for (var i=0;i<400;i++) {
x+=1;
y-=1;
setTimeoue('moveDucks('+ x +','+ y +')',50*i);
}
or
for (var i=0;i<400;i++){
setTimeout('x+=1;y-=1;moveDucks(x,y)',50*i);
}

....
I posted my page at
http://dotdot.sectorlink.org/html/go...cks%20auto.htm if you
would be so kind as to refer to it for the full source. I think all I
need is a way to slow down the above do/while loop.
The do-while loop isn't the problem, it runs as fast as it can.
The problem is that all the setTimeout's have the same delay (500),
so they all happen at (almost) the same time.

In the above, I have picked delays with 50ms intervals. In your code,
you can try changing "500" to "50*x" (since x is already counting from
1 to 400).
I may need more in the function called "newDuckPos" too but I am not
sure, and wouldn't know how to encapsulate the return in setTimeout.


I must admit I am too tired to read that much code right now :)
So, instead, I'll say how I would do it.

Your solution has a big loop that does the computation and creates all
the timeouts at once. I would put the computation into the scheduled
code, so each timeout triggers one update of coordinates, and one
call to the function that moves the ducks to the new coordinate.
Since you know setTimeout, I'll use that. Otherwise you could use
setInterval as well.

---
var x=100,y=600;

function stepDuck() {
x+=1;
y-=1;
moveDucks(x,y);
if (x<400) {
setTimeout(stepDuck,50);
}
}

function seedPos(xaxis,yaxis) {
x=xaxis;
y=yaxis;
setTimeout(stepDuck,50);
}
---

How does that work?

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #10
Thanks Lasse, I finally got it to work. I did take your advice about
using values instead of var names (in certain places.) That eliminated
one class of problem. I also figured out how to call the seedPos
function recursively (with a setTimeout). That totally cleared up the
problem of the moveDucks events firing at once.

Here's what did the trick:
function seedPos(x,y) {

if (x < 710) {
x+=15; y-=15;

moveDucks(x , y);
timer=setTimeout('seedPos('+ x +' ,'+ y ')',500);

}

}

Thanks again for all your help. It was a steep learning curve for this
VB.NET programmer that doesn't know Javascript from Java! ~P

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Randell D. | last post by:
HELP! Its taken me ages - I'm a newbie and I've compiled bits of code that I've read in this newsgroup over time to create one of my most intricate functions to date... Basically, the script...
7
by: Antony Sequeira | last post by:
Hi While looking at some code I realized that the built in setTimeout function takes a string that is later evaluated in the original caller's context. How does one achieve something similar in...
2
by: Athanasius | last post by:
Could someone shed some light as to why the following setTimeout function will not work on the Mac IE5.2? It does however work on PC(Forefox,Netscape,IE) & Mac(Safari,Firefox). Here is the script,...
8
by: VA | last post by:
Suppose I have setTimeout(foo(),n); long_running_function(); Would foo() be executed even when long_running_function() is still executing? In other words, does the setTimeout() pre-empt...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
4
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
15
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first...
3
by: phocis | last post by:
I wrote a pair of functions to enable scoped or referenced setTimeout calls. I did this because I have an object factory that creates multiple objects and those objects need to delay a few calls on...
7
by: Martin | last post by:
Can I have two setTimeouts running at the same time - with two different intervals? I want to start one timer and, before it times out, start another one I've tried this and they seems to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.