3 Replies Latest reply on Mar 29, 2007 10:10 AM by Newsgroup_User

    clearInterval - I'm just not getting it...

    BSpero Level 1
      Hey all,
      I just can't seem to get the clearInterval to work. I start it at the beginning of my movie, and try to disable it while someone is clicking on a slider. I just can't seem to get it at all. I have even just assigned the actions to two buttons to start and stop it, and once it starts, the button to stop it doesn't work. Perhaps I am just missing a piece of punctuation or something. At any rate - shouldn't something as simple as this work... This is in a frame script.

      setInterval(adjustScroll, 20);
      //
      mcSlider.onPress = function() {
      this.startDrag(false, 328, 125, 328, 385);
      clearInterval(adjustScroll);
      };
      //
      mcSlider.onReleaseOutside = mcSlider.onRelease = function () {
      this.stopDrag();
      setInterval(adjustScroll, 20);
      };
        • 1. Re: clearInterval - I'm just not getting it...
          rockbuilder Level 1
          You have to clear the interval id, like this :

          var myinterval:Number=setInterval(adjustScroll, 20);
          var obj=this;
          mcSlider.onPress = function() {
          this.startDrag(false, 328, 125, 328, 385);
          clearInterval(obj.myinterval);
          }

          ++
          • 2. Re: clearInterval - I'm just not getting it...
            Rothrock Level 5
            It is simple and does work, if you do it right! :)

            The setInterval method returns a reference ID for that interval and that is what you need to clear it. Kind of like taking a number in a bakery. The first interval set is 1, then next is 2 and so on. Remember that some things that Flash does internally also use up some of these numbers.

            So the trick is to assign that returned number to a variable and don't reuse that variable until you have cleared it. So for your case:

            myIntervalID=setInterval(adjustScroll,20);

            and then later

            clearInterval(myIntervalID);

            So let's say that the first line returns a 5 so now myIntervalID equals 5. Later the clearInterval looks up the value of myIntervalID and it is 5 so clearInterval knows to stop number 5. You could have also put just this:

            clearInterval(5)

            But since you never know in advance what the the interval ID will be it isn't a good idea to do things like that.
            • 3. Re: clearInterval - I'm just not getting it...
              Level 7
              Just to add to what the rocks have said - you might look at setInterval in
              the Help.

              >>setInterval(adjustScroll, 20);

              Here, you'd be calling the adjustScroll function every 20 ms. That's why it
              doesn't work to call clearInterval using adjustScroll... adjustScroll is a
              reference to a function, not the reference to the interval that you need. As
              the rocks said, when you call setInterval you get a number returned - that's
              the reference you need to pass to clearInterval.


              --
              Dave -
              Head Developer
              www.blurredistinction.com
              Adobe Community Expert
              http://www.adobe.com/communities/experts/