7 Replies Latest reply on Sep 13, 2010 1:28 AM by Flex4Education

    Timer and HSlider, odd when you minimise the browser it runs in

    djh88ukwb Level 1

      Hello all.

       

      I have got a timer that counts from 1 second upto two minutes, (120 seconds) and it suppose to stop.  I have also got a HSLider that is linked to that, which displays how close to the end you are.  This works 100% perfect in a browser as long as you keep the browser, and windows its in active.  If you minimise to something else, the timer carries on, but adds however long it was minimised for onto the end.

       

      So if you minimise the application for a minute, the timer wont stop at two minutes, but in fact three minutes.

       

      A link to it can be found here - http://davidtest.webcastglobal.com/timer/timer.html

       

      The code is here

      <?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" applicationComplete="init()">
          <fx:Declarations>
              <mx:DateFormatter id="dateFormatter" formatString="NN:SS" />
          </fx:Declarations>

          <fx:Script>
              <![CDATA[
                  import flash.events.TimerEvent;
                  import flash.utils.Timer;
                 
                  private const TIMER_INTERVAL:int = 1;
                  private var baseTimer:int;
                  private var t:Timer;
                  [Bindable] public var timerCount:int = 0;
                 
                  private function init():void {
                      t = new Timer(TIMER_INTERVAL);
                      t.addEventListener(TimerEvent.TIMER, updateTimer);
                  }
                  private function updateTimer(evt:TimerEvent):void {
                      var d:Date = new Date(getTimer() - baseTimer);
                      counter.text = dateFormatter.format(d);
                     
                      timerCount++;
                      TimerSlider.value = timerCount;
                      if (timerCount >= 7200)
                      {
                          stopTimer();
                      }
                      if (timerCount >= 6000)
                      {
                          counter.setStyle("color","#cd1d1d");   
                      }
                      else
                      {
                          counter.setStyle("color","#030303");
                      }
                  }
                  private function startTimer():void {
                      baseTimer = getTimer();
                      t.start();
                  }
                  private function stopTimer():void {
                      t.stop();
                  }


              ]]>
          </fx:Script>
         
              <s:Button label="Start timer" click="startTimer()"  x="10" y="10"/>
              <s:Button label="Stop timer" click="stopTimer()"  x="95" y="10"/>
              <s:BorderContainer x="10" y="39" width="350" height="30">
                      <mx:HSlider minimum="0" maximum="7200" verticalCenter="-10" left="11" width="275" id="TimerSlider" liveDragging="false" enabled="true" showTrackHighlight="true" thumbSkin="{null}" allowTrackClick="false" accentColor="#ff0000"/>
                      <mx:Label id="counter" fontSize="14" right="10" textAlign="right" height="30" y="0"/>
              </s:BorderContainer>
         
      </s:Application>

       

      If anyone can offer some insight into how this is happening that would be great !

       

      I really dont know why it adds it onto the end :S

        • 1. Re: Timer and HSlider, odd when you minimise the browser it runs in
          djh88ukwb Level 1

          Ok, after looking into it some more i think i know what my problem is.

           

          I have the timer running which is variable T,

           

          I then have an int, timerCount that is incremented everytime the updateTimer function is run,

           

          This timerCount is what determines when to stop the timer.

           

          But when i minimise the browser, the timerCount stops incrementing.  But the Timer carries on running.

           

          So how do i get the timerCount to carry on running when the browser isnt active ?

           

          Thanks in advance

          • 2. Re: Timer and HSlider, odd when you minimise the browser it runs in
            djh88ukwb Level 1

            Anybody able to help ?  This is really confussing.  The timerCount int stops incrementing when the browser is minimised, but theTime keeps on ticking as it should, which is controlled by the dateformatter.?

             

            Should the timerCount int stop ?  Or is this a bug ?

            • 3. Re: Timer and HSlider, odd when you minimise the browser it runs in
              Flex4Education Level 1

              Actually, your Time doesn't keep on ticking,

               

              it's just the fact, that your time value is calced everytime the timer ticks new.

              It's not calced by a value which is incremented in the TimerEvent. So if you want

              to fix that, you have to calc your HSlider value out uf getTime()-BaseTime() or bind it at your Time value.

               

              F4E

              • 4. Re: Timer and HSlider, odd when you minimise the browser it runs in
                djh88ukwb Level 1

                thanks for the reply !

                 

                The only thing is that sounds very confussing !

                 

                Could you give me a snippit of code please?

                • 5. Re: Timer and HSlider, odd when you minimise the browser it runs in
                  Flex4Education Level 1

                  Hi

                   

                  That means just, that your timer isn't ticking if the application lost the focus.

                  Try to replace

                  timerCount++;

                  with:

                  timerCount = getTimer()-baseTimer();

                   

                  Then it shouldn't matter if your timer ticks alll the time or not.

                   

                  F4E

                  • 6. Re: Timer and HSlider, odd when you minimise the browser it runs in
                    djh88ukwb Level 1

                    Hi,

                     

                    Thanks for the reply.

                     

                    When i changed timerCount++  with timerCount = getTimer() -baseTimer();

                     

                    But i then get the following error

                     

                    Call to a possibly undefined method baseTimer

                     

                    Any ideas?

                     

                    here is the full code

                    <?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" applicationComplete="init()">
                       
                        <fx:Declarations>
                            <mx:DateFormatter id="dateFormatter" formatString="NN:SS" />
                        </fx:Declarations>

                        <fx:Script>
                            <![CDATA[
                                import flash.events.TimerEvent;
                                import flash.utils.Timer;
                               
                                private const TIMER_INTERVAL:int = 1;
                                private var baseTimer:int;
                                private var t:Timer;
                                [Bindable] public var timerCount:int = 0;
                               
                                private function init():void {
                                    t = new Timer(TIMER_INTERVAL);
                                    t.addEventListener(TimerEvent.TIMER, updateTimer);
                                }
                                private function updateTimer(evt:TimerEvent):void {
                                    var d:Date = new Date(getTimer() - baseTimer);
                                    counter.text = dateFormatter.format(d);
                                   
                                    //timerCount++;
                                    timerCount = getTimer()-baseTimer();
                                    TimerSlider.value = timerCount;
                                    if (timerCount >= 7200)
                                    {
                                        stopTimer();
                                    }
                                    if (timerCount >= 500)
                                    {
                                        counter.setStyle("color","#cd1d1d");   
                                    }
                                    else
                                    {
                                        counter.setStyle("color","#030303");
                                    }
                                }
                                private function startTimer():void {
                                    baseTimer = getTimer();
                                    timerCount=0;
                                    TimerSlider.value = timerCount;
                                    t.start();
                                }
                                private function stopTimer():void {
                                    t.stop();
                                }

                               
                                //Text Box
                                protected function txt_change():void {
                                    var currL:String = txt.text.length.toString();
                                    lblmax.text = currL;
                                   
                                }
                               
                            ]]>
                        </fx:Script>
                       
                            <s:Button label="Start timer" click="startTimer()"  x="10" y="10"/>
                            <s:Button label="Stop timer" click="stopTimer()"  x="95" y="10"/>
                            <s:BorderContainer x="10" y="39" width="350" height="30">
                                    <mx:HSlider minimum="0" maximum="7200" verticalCenter="-10" left="11" width="275" id="TimerSlider" liveDragging="false" enabled="true" showTrackHighlight="true" thumbSkin="{null}" allowTrackClick="false" accentColor="#ff0000"/>
                                    <s:Label id="counter" fontSize="14" right="10" textAlign="right" height="30" y="0" verticalAlign="middle"/>
                            </s:BorderContainer>
                            <s:Label x="10" y="77" text="{timerCount}"/>   
                       
                       
                            <s:TextArea id="txt" x="10" y="124" width="350" height="65" maxChars="100" change="txt_change()"/>
                            <s:HGroup x="156" y="197" width="200" height="29" horizontalAlign="right" verticalAlign="middle">
                                    <s:Label id="lblmax" text="0"/>
                                    <s:Label text="/"/>
                                    <s:Label text="100"/>
                            </s:HGroup>

                    </s:Application>

                    • 7. Re: Timer and HSlider, odd when you minimise the browser it runs in
                      Flex4Education Level 1

                      hi

                       

                       

                      Sorry, i should be written like this:

                      getTimer() -baseTimer

                       

                       

                      F4E