7 Replies Latest reply on Jun 22, 2006 4:53 PM by Newsgroup_User

    SetInterval

    300thnickattempt
      Hi, I have this code on an empty movie clip:

      onClipEvent (load) {
      testing = function () {
      trace("OK");
      clearInterval(inID);
      };
      }
      onClipEvent (enterFrame) {
      var inID = setInterval(testing, 1000);
      }

      I want the output to show "OK" once every second. But what's happening is that it's waiting the first 1 second and then just displaying them at the frame rate. It's not doing it on an interval, it's just like a delay until it unleashes its hell on me. This test sums up a much larger problem I'm having with a much more complicated project, but this is the meat of it so can anybody tell me what I did wrong?

      My guess is that the setInterval function is operating, but because it's on EnterFrame, it's calling itself a million times. But I thought that was the point of the setInverval to be able to periodically have something happen in an environment that's calculated 30-60 FPS. In my early defense, every single example of this code has it placed under EnterFrame, just like I have got.

        • 1. Re: SetInterval
          kglad Adobe Community Professional & MVP
          that's a mess. i would think you'd have much greater problems if you let that code run for one or two minuetes.

          first, you're creating a new setInterval() loop with each onEnterFrame loop. second, you're making the interval id (inID) local to your onEnterFrame handler so none of them will clear. in short, you're going to have a lot of 1 second loops in a very short time and those trace() statements will consume more and more computer resources as your swf plays.

          to remedy use:

          • 2. Re: SetInterval
            kglad Adobe Community Professional & MVP
            i just noticed your comment about every example of setInterval() you've seen has it within an onEnterFrame handler. i've rarely seen that and infact, that would make no sense, at all. you'd have to clear each interval before the next is created and you may as well not use setInterval(). just perform a function call from within an onEnterFrame handler if that's what you want.
            • 3. Re: SetInterval
              Level 7
              300thnickattempt,

              I would say your biggest problem is this, right here:


              > onClipEvent (enterFrame) {
              > var inID = setInterval(testing, 1000);
              > }

              The enterFrame event occurs, as its name suggests, every time a frame is
              entered. Even if the playhead isn't moving, an enterFrame event is raised
              at approximately the rate of the movie's frames per second.

              > In my early defense, every single example of this code
              > has it placed under EnterFrame, just like I have got.

              Heh, well, then every single example you've seen is gravely mistaken.
              ;) But don't worry. No need for an early defense, or a defense of any
              kind. The setInterval() function already *is* a looping mechanism. It
              triggers the function you supply at an interval you supply. In this case,
              there must be a function named testing() within scope of this call to
              setInterval(). The testing() function will be invoked every 1000
              milliseconds (that is, every second).

              By putting this line into an enterFrame event handler, you're creating a
              *new* 1-second loop interval about 30 times a second (assuming your FPS is
              30). In ten seconds, that's 300 new 1-second intervals -- and it won't stop
              there, of course.

              You'll want to put this setInterval() function in a place where it will
              only be called once. You can put it in a frame of the main timeline (or any
              timeline) -- or you can put it in the load event handler of that same movie
              clip.

              Make sense?


              David
              stiller (at) quip (dot) net
              Dev essays: http://www.quip.net/blog/
              "Luck is the residue of good design."


              • 4. Re: SetInterval
                300thnickattempt Level 1
                Thanks guys, that's exactly what I was hoping for. It explains everything and it is working for me now. I knew that it felt weird continually calling the setInterval on the enterframe and yes, I was wrong in saying that I've seen it in enterFrames. The truth is that I referenced this from a text book and assumed the code in the figures was from one continuous source but the captions said it was in the frame, not the MC. My dumb fault. But thanks again to the both of you, that was outstanding help.
                • 5. Re: SetInterval
                  300thnickattempt Level 1
                  I just wanted to add one more thing that I noticed, if the name of the interval doesn't end with "ID" (example testingID or mystuffID), the interval only goes through once. That I didn't know, but I just tested it. I guess "ID" is a big keyword in the name.
                  • 6. Re: SetInterval
                    Level 7
                    300thnickattempt,

                    > I just wanted to add one more thing that I noticed, if
                    > the name of the interval doesn't end with "ID" (example
                    > testingID or mystuffID), the interval only goes through
                    > once.

                    What!?

                    > That I didn't know, but I just tested it. I guess "ID" is
                    > a big keyword in the name.

                    Honestly, that has nothing to do with it. The setInterval() function
                    provides a return value, as do many functions and methods in ActionScript.
                    The value it returns is simply a number. You can store that number in any
                    variable you like, named whatever you please, provided it's a legally formed
                    name.

                    All clearInterval() needs as its parameter is a number. That's what
                    your variable provides. The variable doesn't store an "interval," so much
                    as it stores the number (an "ID") of a particular interval. The first one
                    is 1, the second 2, and so on. If you wanted, you could put numbers
                    directly into clearInterval() and it would do the same thing -- it's just
                    that a variable means you don't have to keep track.


                    David
                    stiller (at) quip (dot) net
                    Dev essays: http://www.quip.net/blog/
                    "Luck is the residue of good design."


                    • 7. Re: SetInterval
                      Level 7
                      >I just wanted to add one more thing that I noticed, if the name of the
                      >interval
                      > doesn't end with "ID" (example testingID or mystuffID), the interval only
                      > goes
                      > through once. That I didn't know, but I just tested it. I guess "ID" is a
                      > big
                      > keyword in the name.

                      That is completely incorrect. .the name of the variable has NOTHING AT ALL
                      to do with how the interval works.

                      You must be doing something else wrong.

                      You are probably directly calling the function, instead of passing it to
                      setInterval. If you have a () after the function name when you pass it to
                      setinterval. That will execute it once immediately and NEVER on the
                      interval.
                      --
                      jeckyl