2 Replies Latest reply on Jun 23, 2010 10:39 PM by vineet osho

    how to create record timer in flex ?

    vineet osho Level 1

      Hello Friends

       

         i  stuck in a problem...  i want to make an timer which starts as soon as i click a button .. i am not able to make program for this.

       

        The format will be

       

      00:00:00

       

      if it is runnuing from last 1 min and 22 seconds then the timer shows like this

       

       

      00:01:22  

       

      like it keeps on running until i hit the stop button to stop it.

       

      Plz guide me how can i achieve this using flex...

       

       

       

        Regards Osho

        • 1. Re: how to create record timer in flex ?
          David_F57 Level 5

          hi,

           

          The following is a simple timer, you could get more complex by actually comparing the start time (date) with current time(date) but just to count secs this does the trick

           

          David.

           

          <?xml version="1.0" encoding="utf-8"?>

          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

             xmlns:s="library://ns.adobe.com/flex/spark"

             xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">

           

          <fx:Script>

          <![CDATA[

          import mx.events.FlexEvent;

          private var timer:Timer = new Timer(1000);

          private var timeLapse:uint = 0;

           

          protected function application1_creationCompleteHandler(event:FlexEvent):void

          {

          timer.addEventListener(TimerEvent.TIMER,onTimer);

          }

           

          private function onTimer(event:TimerEvent):void

          {

          timeLapse += 1;

          tl.text = timeToString(timeLapse);

           

          }

           

          public function timeToString(value:Number):String

          {

          var seconds:Number  = value % 60;

          var minutes:Number  = Math.floor((value/60) % 60);

          var hours:Number  = Math.floor((value/3600) % 24);

           

          var s:String = seconds < 10 ? "0" + String(seconds) : String(seconds);

          var m:String = minutes < 10 ? "0" + String(minutes) : String(minutes);

          var h:String = hours < 10 ? "0" + String(hours) : String(hours);

           

          return h + ":" + m  + ":" +  s;

          }

           

          protected function button1_clickHandler(event:MouseEvent):void

          {

          timeLapse = 0;

          timer.start();

          }

           

           

          protected function button2_clickHandler(event:MouseEvent):void

          {

          timer.stop();

          }

           

          ]]>

          </fx:Script>

          <s:Button x="58" y="34" label="Start" click="button1_clickHandler(event)"/>

          <s:Button x="136" y="34" label="Stop" click="button2_clickHandler(event)"/>

          <s:Label id="tl" x="58" y="89" width="148" fontSize="16" fontWeight="bold" textAlign="center" color="#102EE2"/>

          </s:Application>

          • 2. Re: how to create record timer in flex ?
            vineet osho Level 1

            Thanks David It works.....