4 Replies Latest reply on Nov 30, 2008 2:43 PM by rrhyne

    Using the Timer

    Tones411
      I'm trying to use the timer. I can create a timer but now I want to actually do something when the time is up. I'm not sure how exactly to access or use the Timer.timer event. In the code below I get the error: "Can't find the variable: flash". Any suggestions?

      I've been on these two pages:

      http://livedocs.adobe.com/air/1/jslr/flash/utils/Timer.html#event:timer
      http://livedocs.adobe.com/air/1/devappshtml/help.html?content=splash.html

      Sample Code:

      var TheTimer = false;

      function timerInit(){
      TheTimer = new air.Timer(3000, 0);
      TheTimer.start();
      window.nativeWindow.addEventListener(flash.events.TimerEvent.TIMER, onTimer);
      }

      function onTimer(){
      window.nativeWindow.visible = true;
      }
        • 1. Re: Using the Timer
          enorton@adobe Level 1
          Hi Tones411,

          I modified things a tiny bit:

          <html>
          <body>
          <script>
          <!--

          var trace = window.runtime.trace
          var TimerEvent = window.runtime.flash.events.TimerEvent
          var Timer = window.runtime.flash.utils.Timer

          var theTimer;

          function timerInit(){
          trace("in timerInit()");
          theTimer = new Timer(3000, 0);
          theTimer.addEventListener(TimerEvent.TIMER, onTimer);
          theTimer.start();
          trace("timer started");
          }

          function onTimer(){
          trace("timer triggered");
          theTimer.stop();
          theTimer.removeEventListener(TimerEvent.TIMER, onTimer);
          }


          -->

          </script>

          <input type="button" onClick="timerInit()" name="startTimer" id="startTimer" value="Start Timer">

          </body>
          </html>

          I hope it helps!
          -Erica
          • 2. Re: Using the Timer
            Tones411 Level 1
            Thank you!

            I was able to get the timer event to work.

            What is the difference between air.Timer and window.runtime.flash.utils.Timer?
            • 3. Re: Using the Timer
              Tones411 Level 1
              I think I know the difference now. Apparently air.Timer is just an alias for window.runtime.flash.utils.Timer.

              You can use the short versions of things such as air.Timer as well as a lot of other things if you include AIRAliases.js in your project.
              • 4. Re: Using the Timer
                rrhyne Level 1
                I realize this post is old, but to add to info on the topic, if you want to reuse the timer,
                don't use:

                theTimer.stop();

                instead use:

                theTimer.reset();

                ...as it sets the timer to zero, and stops the timer.

                and don't remove the listener.