4 Replies Latest reply on Aug 21, 2009 9:41 AM by ckpepper02

    setInterval() not consistent

    ckpepper02

      Hello folks!

       

      I am creating a slideshow using setInterval to slide from one image to the next. It works great the first go around, but then the time seems to be cut in half for the first slide and normal for the second.

       

      Here is my code:

       

      e = 1;
      timeout = setInterval(autoSlide, 2000);
      
      function autoSlide(){
      clearInterval(timeout);
      TweenLite.to(theShow.loadpic, 1, {_x:posAry[e], ease:Expo.easeInOut});
      if (e <= limit){ e++;}
      else {e = 0;}
      timeout = setInterval(autoSlide, 2000);
      }
      

       

      There are only 2 pictures at the moment. When I first run it, the time is perfect, when it loops back to the first picture, the time is much too short, but fine on the second picture.

       

      Any ideas?

        • 1. Re: setInterval() not consistent
          Rothrock Level 5

          Well I'm not sure exactly what all the limit bit and stuff and such is, but my guess is that you are setting a new interval before the old one has been cleared and after that you are getting two called with a bit of an offset in their "start times."

           

          My first recommendation is that you never use setInterval without first doing a clearinterval. No matter how clever a system you think you've come up with the are just too many ways, especially if you loop a timeline, that you can end up with multiple intervals. And if you are only using one variable to hold the reference id, well, then you will never be able to clear the bogus one.

           

           

          e = 1;

          clearInterval(timeout);

          timeout = setInterval(autoSlide, 2000);

           

          function autoSlide(){

          clearInterval(timeout);

          TweenLite.to(theShow.loadpic, 1, {_x:posAry[e], ease:Expo.easeInOut});

          if (e <= limit){ e++;}

          else {e = 0;}

          clearInterval(timeout);

          timeout = setInterval(autoSlide, 2000);

          }

           

          The second one there is probably redundant since you call it right inside the function definition, but that is what I do. Always clear then set. It just makes an easy rule to follow and saves a lot of headache.

          • 2. Re: setInterval() not consistent
            ckpepper02 Level 1

            Thanks for your response Rothrock.

             

            I tried what you suggested, but there was no change.

             

            FYI, the limit variable is set by my XML for the amount of pictures I have.

            • 3. Re: setInterval() not consistent
              Rothrock Level 5

              Well that was the most likely cause and a good thing to always do in any event. The next step I would probably do is add a trace to the function like this:

               

              trace("current time is: "+getTimer());

               

              And see if the function is getting called more often than about every 2 seconds. If it is then you've got some problem somewhere else because this code is pretty simple.

               

              Is the problem that you are not taking into account the 1 second for your tween?

              1 person found this helpful
              • 4. Re: setInterval() not consistent
                ckpepper02 Level 1

                I got it!

                 

                My variable "limit" was being set incorrectly in the XML portion of my AS code. That was throwing off the slide time. What I thought was the correct timing for the second picture, was actually extra time being put added in.

                 

                Thanks for the help Rothrock!