3 Replies Latest reply on May 4, 2006 10:35 AM by Newsgroup_User

    using setInterval/clearInterval to cycle through visible-invisible

    Level 7
      I've got so confused with setInterval/clearInterval that fail to code a very
      basic effect.
      I need an object to become visible for, say 3 sec, then to become invisible
      for 2 sec and so far.
      I did three functions:

      function objOnAndOff(){
      int1=setInterval(objOff,3000);
      int2=setInterval(objOn,4998)
      }

      function objOff(){obj._visible=false;clearInt(int1)}
      function objOn(){obj._visible=true;clearInt(int2)}

      obj._visible=true;
      var int1,int2,int3

      int3=setInterval(objOnAndOff,5000)

      I expected objOnAndOff be called every 3+2=5 sec, of which the object would
      stay visible for the first 3 sec, then on 3.001 objOff() sets it invisible,
      then on 4.999 objOn() sets it visible. Immediately after that objOnAndOff()
      is called again and the cycle repeats. Instead it behave erraticaly.

      Where does my logic fail?



        • 1. Re: using setInterval/clearInterval to cycle through visible-invisible
          pwyon Level 1
          You could try something like this and only use one "setInterval()" and "getTimer()".
          Just put the code on the main timeline on a layer called: Actionscript
          On another layer create three boxes and give them instance names of: myClip1 , myClip2 , myClip3.

          Do a test run and see what happens. It should work. I hope this is what you were looking for. I'm not sure if I totally understand your question.

          //...
          • 2. Re: using setInterval/clearInterval to cycle through visible-invisible
            kglad Adobe Community Professional & MVP
            you have a syntax error and an error in flash-logic. to correct the syntax error use clearInterval() to clear your intervals (clearInt() is not a flash function).

            your error in flash-logic is thinking setInterval() function calls are precise to within 2 ms. they are not and you're likely to accumulate uncontrollable calls to objOn().

            i don't think pwyon's code will do what you want. try:
            • 3. Re: using setInterval/clearInterval to cycle through visible-invisible
              Level 7
              Thanks. Amaizingly, I used clearInterval() many times and yet wrote
              clearInt() looked at is many times and failed to notice.
              I works in general.
              However I have a timer counting down every second and I planted trace()
              into your two functions - the interval shown by the times is not stable. It
              is suppose to be 2 and 3 but timer sometimes shows 1 nd 2 respectively -
              Iis is cvorrect to suggest that this is the result of an inacuracy
              accumulated over several cycles and we have this leap year regularly?

              Also is it important that you clearInterval for Off/On BEFORE setting the
              interval for On/Off
              In my first attempt I coded similar functions but clearInterval for Off/On
              AFTER setting the interval for On/Off and it did not work, though I do not
              see why this matters.

              "kglad" <webforumsuser@macromedia.com> wrote in message
              news:e3bqes$g1p$1@forums.macromedia.com...
              > you have a syntax error and an error in flash-logic. to correct the
              syntax
              > error use clearInterval() to clear your intervals (clearInt() is not a
              flash
              > function).
              >
              > your error in flash-logic is thinking setInterval() function calls are
              precise
              > to within 2 ms. they are not and you're likely to accumulate
              uncontrollable
              > calls to objOn().
              >
              > i don't think pwyon's code will do what you want. try:
              >
              >
              > function objOff(){
              > obj._visible=false;
              > clearInterval(offI);
              > onI=setInterval(objOn,2000);
              > }
              > function objOn(){
              > obj._visible=true;
              > clearInterval(onI);
              > offI=setInterval(objOff,3000);
              > }
              > objOn();
              >