setTimeout导致返回3200万而不是x,y坐标 [英] setTimeout causes return of 32 million instead of x,y coordinate

查看:65
本文介绍了setTimeout导致返回3200万而不是x,y坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在屏幕上移动图片,稍微延迟

在第一张图片和第二张图片之间(跟随onmousemove)。

当我添加setTimeout函数我必须做错了因为

计算图片新坐标的函数

返回3200万并更改而不是坐标。显然,这会导致图片无法显示在显示屏上。


这里是调用代码(为了便于阅读而剪切)没有

setTimeout:


*************************** ******

函数moveDucks(xPos,yPos){


baby1.left = newDuckPos(yPos - 30,xPos - 30)

baby1.top = newDuckPos(xPos + 30,yPos + 30)

}

函数newDuckPos(currentPos,duckPos){


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


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


}

*************************** *************


以上返回正确的newPos并且一切都很笨拙。

然后我

尝试使用setTimeout:

************* *******************************

函数moveDucks(xPos,yPos){

baby1.left = setTimeout(" newDuckPos(" + yPos +" - 30," + xPos +" -

30)",500); < br $> b $ b clearTimeout;


baby1.top = setTimeout(" newDuckPos(" + xPos +" + 30," + yPos +" +

30)",500);

clearTimeout;

}

******** ****************************** *


以上原因导致newDuckPos返回3200万+而不是

正常的corrdinates。我对Javascript过于陌生,无法找到线索。我跟b / b确实跟随i / o确实看到坐标在到达newDuckPos时有有效的

值(我输出它们来显示

肯定)。然后newDuckPos计算它们并在它们发回之前

我再次拦截它们。再一次,价值观看起来很好。


然后newDuckPos返回它们并且它们被淹没。


任何人都可以引导我朝着正确的方向前进这个?

谢谢。

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.

推荐答案



" Jupiter49" <菊******* @ msn.com> schreef in bericht

新闻:c7 ************************** @ posting.google.c om ...

"Jupiter49" <ju*******@msn.com> schreef in bericht
news:c7**************************@posting.google.c om...
我正试图在屏幕上移动图片,在第一张图片和第二张图片之间稍有延迟
(在onmousemove之后)。
当我添加setTimeout时函数我必须做错了因为
计算图片新坐标的函数
返回3200万而改变而不是坐标。这导致图片无法显示在显示器上。


当说出类似的内容时:

baby1.top = setTimeout (....);
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将保留对setTimeout调用的引用,而不是新的

偏移量。


尝试以下(看评论):


函数moveDucks(xPos,yPos){

// statement + varname确保varname不被视为字符串

//所以,当xPos eq 1时,xPos + 30不会变成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



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

Janwillem Borleffs写道:

Janwillem Borleffs wrote:


当说出类似的内容时:


When saying something like:

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



baby1.top将持有


baby1.top will hold



setTimeout,jah返回的计时器ID值?


dag,

Dom


the timer id value returned by setTimeout, jah?

dag,
Dom




Dom Leonard <做************* @ senet.andthis.com.au> schreef in bericht

news:fR ***************** @ nnrp1.ozemail.com.au ...

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


setTimeout返回的计时器id值,jah?


the timer id value returned by setTimeout, jah?




这是一个参考...


JW



Which is a reference...

JW


这篇关于setTimeout导致返回3200万而不是x,y坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆