4 Replies Latest reply: Mar 21, 2013 4:28 AM by Andrei1 RSS

    Make a timer

    mentalcase129 Community Member

      I have a digital timer counting down in the background of a website I'm working on.  It's strictly an aesthetic thing and doesn't really serve any real purpose otherwise.  I just want it to look like a working timer.  Is there a way I can do this with actionscript so that I don't have to actually keyframe the numbers changing?  I'm actually more of an animation guy and would usually just animate it timing down but that's probably more work than it's worth.

      thanks

        • 1. Re: Make a timer
          Ned Murphy CommunityMVP

          Search Google or even these forums using terms like "AS3 countdown tutorial"

          • 2. Re: Make a timer
            Andrei1 Community Member

            The example below displays countdown from 2 minutes to zero.

             

             

            import flash.display.Sprite;
            import flash.events.TimerEvent;
            import flash.text.TextField;
            import flash.text.TextFormat;
            import flash.utils.Timer;
            
            var countDownDisplay:TextField;
            var timer:Timer;
            var date:Date;
            
            init();
            
            function init():void
            {
                      countDownDisplay = new TextField();
                      countDownDisplay.defaultTextFormat = new TextFormat("Arial", 120, 0x008040);
                      countDownDisplay.multiline = countDownDisplay.wordWrap = false;
                      countDownDisplay.border = true;
                      countDownDisplay.autoSize = "left";
                      countDownDisplay.x = countDownDisplay.y = 20;
                      countDownDisplay.text = "00:00 ";
                      addChild(countDownDisplay);
                      date = new Date(1000 * 120);
                      timer = new Timer(500);
                      timer.addEventListener(TimerEvent.TIMER, onTimer);
                      timer.start();
            }
            
            function onTimer(e:TimerEvent):void
            {
                      date.time -= timer.delay;
                      countDownDisplay.text = date.toUTCString().match(/\d\d:\d\d\s/)[0];
                      if (date.time <= 0)
                      {
                                timer.stop();
                      }
            }
            
            
            • 3. Re: Make a timer
              mentalcase129 Community Member

              Would you be able to tell me which part of this coded dictates the start time?  So like if I wanted to change it a higher number than 2 minutes?

              • 4. Re: Make a timer
                Andrei1 Community Member

                The line

                 

                date = new Date(1000 * 120);

                 

                sets it to 2 minutes.