1 Reply Latest reply on Apr 30, 2007 5:14 AM by dr_ross

    set Interval

    fourinchesofego
      Hello...

      I have written a small function that simply moves an object to a new position on stage depending on a variable passed to the function. It works just fine, but instantly rather than in intervals -- mimicking a tween. I was wondering how I would integrate a delay into this function to allow it to work in increments.

      The function is as follows:
      function moveIt(thePosition) {
      while (theName._x != thePosition) {
      if (theName._x<thePosition) {
      theName._x += 5;
      } else if (theName._x>thePosition) {
      theName._x -= 5;
      }
      }
      }

      Can I use set Interval? If so, how? Would I strip out the while loop to do so?

      Help would be greatly appreciated.

      Thanks
        • 1. Re: set Interval
          dr_ross Level 1
          At a guess of what you're after something like this should help

          function moveIt(thePosition) {
          //while (theName._x != thePosition) {
          if (theName._x<thePosition) {
          theName._x += 5;
          } else if (theName._x>thePosition) {
          theName._x -= 5;
          }
          //}
          }
          my_interval = setInterval(this,"moveIt",100,100);

          where the first 100 is the millisecond gap between calls, the second is the position parameter passed to the function

          to stop the interval call clearInterval(my_interval);