3 Replies Latest reply on Aug 2, 2007 1:31 PM by Rothrock

    setInterval repeat when finished looping forever

    code1234
      Greetings,

      The following setInterval example is right out of the Flash documentation, and I'm using this method to periodically, create, animate and fade out (using the tween class) some items on screen

      This code from the docs traces the contents of an array, passed as parameters, at one second intervals. What is the best way to have it start again when completed, thus beginning again to trace the first parameter-- and so on forever looping.

      Thanks in advance to all you AS experts.

        • 1. Re: setInterval repeat when finished looping forever
          Rothrock Level 5
          The whole point of setInterval is that is repeats forever at the duration you set – or until it is cleared. So if you want this to keep repeating remove the clearInterval. Also I'm not sure what is going on with the conditional, it seems to be incrementing each time, and then when it has gone through the array once it won't reset the interval anymore.

          Instead of hard coding the maxCount, I would use the actual length of the array. And instead of using it to decide when to stop your interval I would use it to check when you needed to start over again:

          count++
          if(count>=param.length){
          count=0;
          }
          • 2. Re: setInterval repeat when finished looping forever
            code1234 Level 1
            Rothrock, That did it.

            Thanks for the reply. I changed the clearInterval to count = 0, and now on and on it goes.

            It seems so simple after your explanation. Not sure why, but even after pouring over the Flash docs on this, I'm still having trouble getting my head around using setInterval.

            Anyway, thanks again.
            • 3. Re: setInterval repeat when finished looping forever
              Rothrock Level 5
              Yes, it does seem to be one of those things that a lot of folks struggle with – I did too way back.

              Just remember that it means, "keep doing what I tell you at the agreed upon interval – until you are told to stop." I think assigning the return value to a variable is a big part of the confusion. Really it is just like getting a ticket in a bakery. There can be many interval running at the same time, so to keep them straight flash gives them a number – the first one is 1, the next 2, and so on.

              So all that we are doing with the assigning to a variable is pulling the bakery ticket out of the machine and hanging on to it. There is no way to know what the number will be until we pull it out or what it was if we lose it. So we have to keep it around (by putting it in a variable). Then later we can say, "Oh yeah, remember interval 5, well cancel it now."

              Remember too, that the internal Flash classes may also use setInterval so you can never count on, "Well the last interval I used was 4, so this next one will be 5."