7 Replies Latest reply: Aug 2, 2012 2:38 PM by 3dcruiser RSS

    Actionscript 2 - stop a function

    3dcruiser Community Member

      I have a function to make a number count up tp sixty. I want it so if you leave the frame the counter will reinitiate on return. As it is, the counter works fine the first time through, but runs faster everytime you return to frame. Here is the code.

       

      textValue = 10;
      addValue = 1;
      txt_ET.text = textValue;


      function countUp(){
        clearInterval(Interval);

      if (textValue < 60){
      clearInterval(Interval);
      textValue += addValue;
      txt_ET.text = textValue;
       
      }

      }

      setInterval(countUp,800);

        • 1. Re: Actionscript 2 - stop a function
          prodigalmaster Community Member

          Is the name of the setInterval 'Interval'? Are you actually clearing it, since setinterval's name hasn't been declared.

          • 2. Re: Actionscript 2 - stop a function
            3dcruiser Community Member

            I am unclear as to how setInterval and clearInterval work. I assumed setInterval at bottom set the delay for the function called countUp and setInterval just cleared the delay. if I put clearInterval(countUp); I get error message.

            • 3. Re: Actionscript 2 - stop a function
              Ned Murphy CommunityMVP

              You should look in the help documentation regarding the use of setInterval and clearInterval to learn how they work. 

               

              If you want to clear an interval you need something to identify the interval.   When the setInterval function executes, it returns a Number value that is such an identifier.  You use that number in the clearInterval function call.  So for what you show above, your setInveral line should look something like...

               

                 var Interval:Number = setInterval(countUp, 800);

               

              then your clearInterval function call as shown should work... except you have a redundant calling of it within the function... one of the two shown is unnecessary - I would guess the first...

               

                 function countUp(){

                    clearInterval(Interval);

               

                    if (textValue < 60){

                         clearInterval(Interval);

              • 4. Re: Actionscript 2 - stop a function
                3dcruiser Community Member

                it is still not working. I think I need to rethink this approach. who knew just getting a counter to increment or reset upon returning to frame would be such  trouble. 

                • 5. Re: Actionscript 2 - stop a function
                  Ned Murphy CommunityMVP

                  I don't see how setInterval works into managing a counter when you are using a frame-based design.  Changing frames should be sufficient as far as whatever your counting is dealing with.

                  • 6. Re: Actionscript 2 - stop a function
                    3dcruiser Community Member

                    I guess I should explain the project i'm editing. I am editing an Actionscript 2 project that is screen based. I have this code in a movie clip. Inside this movie is a text object that I want to be at ten and count up to 60. I also have a button so the viewer can advance to the next screen if He doesn't want to wait for the number to get to 60. The problem is that if the viewer hits the "NEXT" button and goes to the next screen and then decides to go back I want the counter to reset to 10. Currently, it runs great the first time through, but if you advance to next screen early and come back the counter keeps going where it left off. It doesn't reset.

                    • 7. Re: Actionscript 2 - stop a function
                      3dcruiser Community Member

                      Well, for now I just made animation with 50 keyframes. It's clunky,but it works. Still would like to do with actionscript, but for now I can move on. Thanks for trying.