...

View Full Version : short script, need fresh eyes, seems fine, possibly not clearing timeout



subhailc
08-13-2004, 09:09 AM
hi. everything works fine, but for some reason i think im not clearing timer1; is my conditional statement poorly done? i thought i was saying "if the element's vertical postion is equal to or greater than the specified position, clear the timer. otherwise, if the element's vertical position is less than the specified position, run the function again and then check." am i saying something else?

should i be using a for loop, or while? what about just setting up another timer to clear that timer - bad idea?

i tried switching around the comparison operators, and tried using um i forget the names... logical something? um anyways these things: == and !== instead, with no obvious effect.

thanks.



<script language="javascript">
var timer1=null;
px = (document.getElementById) ? "px" : "";
function gP(id) {
endPos = document.getElementById(id).offsetTop;
}
function gM(obj) {
el = document.getElementById(obj).style ;
el.ypos = parseInt(el.top);
if (el.ypos >= endPos) {
clearTimeout(timer1);
}
else if (el.ypos < endPos) {
clearTimeout(timer1);
}
distance = endPos - el.ypos + (-200);
el.ypos += distance*.2;
el.top = el.ypos+px ;
timer1= setTimeout("gM('" + obj + "')",10);
}
</script>

Kor
08-13-2004, 09:55 AM
It looks like you fire the clearTimeout for all the situations...



if (el.ypos >= endPos) {
clearTimeout(timer1);
}
else if (el.ypos < endPos) {
clearTimeout(timer1);


how can you suppose that the object will move if nomatter the value of it's position (>= or <) you chose to clearTimeout?

To move an object you should have something like

...
if(el.ypos>=endPos){clearTimeout()}
else{setTimeout()}
...

That will produce a movement from left to right, if the object's y position is initially smaller that the limit.


um anyways these things: == and !== instead, with no obvious effect.


They are == and !=, but in movement function usualy are used >=, <=, <, >, == according to the needs

subhailc
08-13-2004, 10:38 AM
hmm... thanks for the reply kor...

everything does move... that's not the problem... i'm not even positive there is a problem... it looks as if it works perfectly, i'm only concerned because my processor and page file use spikes when i run it... but my computer is old and slow so perhaps is shouldn't worry...

the clearTimeout is called at the end of the function, to restart the cycle... and the == and != were just swapped in place of the >= and < to see if that helped; it didnt' help but didnt hurt either, the movement still occurred at the correct times and frequencies.... i'll try omitting the second clear time out and see if that helps, but it hasn't in any way affected the animation; perhaps setInterval would be better? - again, there isnt anything obviously wrong - the script runs fine...

and x is horizontal, y is vertical :)

Kor
08-13-2004, 11:19 AM
Sorry about the x-y confusion :D

Even I don't see how your script can move something (if and else if should clearTimeout in all cases)... anyway... In this case, I don't see which is really your problem?

dumpfi
08-13-2004, 05:45 PM
Although he is clearing the timeout in any case he also sets a new timeout in any case. That's why it works. ;)

<script type='text/javascript'>
var timer = null;
var px = (typeof(document.getElementById) != 'undefined') ? 'px' : '';
function gP(id) {
endPos = document.getElementById(id).offsetTop;
}
function gM(obj) {
var el = document.getElementById(obj).style;
if(parseInt(el.top) >= endPos) window.clearTimeout(timer);
else {
el.top = parseInt(el.top) * 0.98 + endPos * 0.2 - 40 + px;
timer = window.setTimeout("gM('" + obj + "')",10);
}
}
</script>
dumpfi



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum