473,769 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(curr entPos, duckPos) {

newPos = Math.min(Math.m ax(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("new DuckPos("+yPos+ " - 30, "+xPos+" -
30)",500);
clearTimeout;

baby1.top = setTimeout("new DuckPos("+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 3068

"Jupiter49" <ju*******@msn. com> schreef in bericht
news:c7******** *************** ***@posting.goo gle.com...
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("bab y1.left = newDuckPos("+yP osDecr+", "+xPosDecr+")", 500);
setTimeout("bab y1.top = newDuckPos("+xP osIncr+", "+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.de mon.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("",1 00) 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("",1 00) 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('mov eDucks('+ 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('mov eDucks('+ 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(step Duck,50);
}
}

function seedPos(xaxis,y axis) {
x=xaxis;
y=yaxis;
setTimeout(step Duck,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

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

Similar topics

2
5368
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 gets called with a single arguement (full path to an image). The image is supposed to be downloaded to the cache, and when complete, a new window opened that is slightly larger insize then the images dimensions... The new window will contain the...
7
1646
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 user defined functions. -Antony
2
4672
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, it should simply present an alert message after 5 seconds. Thanks. <html> <head> <script language="javascript"> function MsgTimer() { var self = this;
8
15102
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 anything else?
12
5556
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. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
4
5234
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 javascript function which calls setTimeout and will process a second javascript function "Warn" just before the session expires. The Warn function displays an html page with a button. A second timer is started to cause the html page to close...
15
3796
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 image rotates. I cannot figure out how to accomplish the 3 second delay. My code is pasted below: function randPic(){ randPic1(); randPic2();
3
2802
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 themselves at a certain point in time. This code works fine for normal objects. However, my issue occurs if at any point, an event object is passed across in IE. It seems, that events methods and variables in IE are PRIVATE (!) after the...
7
2415
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 interfer with one another.
0
9590
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9424
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.