3 Replies Latest reply on Jun 11, 2009 6:02 AM by clbeech

    Resetting Cleared Intervals?

    shintashi Level 1

      Basic problem. Testing methods of turning intervals on and off. I have an interval that works, and can be turned off, but the button to turn in back on is failing. Basically when this program is turned on, a bar rotates 5 degrees each second and a counter increases by +1. When the "a" button is pressed, the interval stops (good). When the "b" button is pressed, the interval doesn't restart (bad).

       

      What am I doing wrong?

       

       

      -----

      jim = 1;

       

       

       

      var ticker;

       

      ticker = setInterval( function(){bar_mc._rotation += 5;
                              casper.text = 0 + jim;
                              jim = jim +1;  
                                 }, 1000 );

       

         
          keyListener = new Object();
          keyListener.onKeyUp=function(){
         var keyUsed = Key.getCode();
      //a
         if (keyUsed == 65){
      //clearInterval;
                 }
              
      //b        
      else if (keyUsed == 66){
      //clearInterval;
             }
            
          }

       


      keyListener.onKeyDown=function(){
         var keyUsed = Key.getCode();

       

      //a
         if (keyUsed == 65){
      clearInterval(ticker);

       

                 }
              
      //b        
      else if (keyUsed == 66){
      setInterval(ticker);
          }
       
      }
      Key.addListener(keyListener);

      -----

        • 1. Re: Resetting Cleared Intervals?
          clbeech Level 3

          ok - first, you're assigning an 'anonymous' function to the setInterval method, although this is fine it does not 'persist' - each time you declare the interval again, it needs a 'new' function assignment - this is the main issue.  to remedy, move the function to a method outside the setInterval declaration, and reassign the method when 're-acitvating' the interval like this:

           

          var count:Number = 0;
          var timer:Number;
          
          var keyListener:Object = new Object();
          Key.addListener(keyListener);
          
          keyListener.onKeyDown = function():Void {
              var keyUsed = Key.getCode();
              if(keyUsed == 65) stopTimer();
              if(keyUsed == 66) startTimer();
          }
          
          function startTimer():Void {
              clearInterval(timer);
              timer = setInterval(counter, 1000);
          }
          
          function stopTimer():Void {
              clearInterval(timer);
          }
          
          function counter():Void {
              casper.text = count++;
              bar_mc._rotation += 5;
          }
          
          • 2. Re: Resetting Cleared Intervals?
            shintashi Level 1

            so if I am to understand this correctly,

             

            the motions & counting are inside the counter function

            the counter is inside the the timer/set interval

            the timer/set interval is inside the clear interval

            the clear interval is inside the start timer & inside the stop timer

            and the start & stop timers are inside the A & B key listeners?

             

            this makes my head hurt but I sort of get it.

            • 3. Re: Resetting Cleared Intervals?
              clbeech Level 3

              lol   kinda yes.  but think of it this way -

               

              "the motions & counting are inside the counter function"

              - yes these items are 'handled' by the the counter method.

               

              "the counter is inside the the timer/set interval"

              - not exactly 'inside', each time the iterval is fired it 'calls' the assigned method.


              "the timer/set interval is inside the clear interval"

              - not really, the setInterval method returns a 'number' that is used as an ID for that particular interval.  the ID is 'stored' as a reference to the interval in the variable property as we have assigned it - in this case the property 'timer'.  the clearInterval method 'stops' the currently operating interval that the reference is pointing to - so it uses the ID to target that particular interval, but the property and the 'setInterval' are not really 'inside' the method.

               

              "the clear interval is inside the start timer & inside the stop timer"

              - clearInterval is used when you want to cancel or stop a currently running interval, it is a best practice to call clearInterval previous to setting a 'new' interval so as to make certain that the current ID reference is 'not' running.  so in the startTimer method it is called to assure that the interval is cleared, in the stopTimer method it is called simple to cancel/stop the interval.


              "and the start & stop timers are inside the A & B key listeners?"

              - key press 'triggers' or 'calls' the startTimer/stopTimer methods, but they do not reside 'inside' the listeners, they are only 'activated' by calling the method.