Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post

    short script, need fresh eyes, seems fine, possibly not clearing timeout

    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.

    Code:
    <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>

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    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

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Sorry about the x-y confusion

    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?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Although he is clearing the timeout in any case he also sets a new timeout in any case. That's why it works.
    Code:
    <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
    Last edited by dumpfi; 08-13-2004 at 05:51 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •