2 Replies Latest reply on Jan 21, 2008 9:27 AM by m_hartnett

    clock killer

    Tigomark Level 1
      Hello,

      I have this clock that runs as a countdown timer once a user logs into
      the site. It works great until the user logs out. When they log back
      in the counter no longer counts correctly. It is like there are 2
      intstances of the clock running at the same time but I'm not sure how
      to kill the first instance.

      Clock code
      Code:

      private function getDaysUntil():void {
      //setting the timer
      var minuteTimer:Timer = new Timer(1000, 300);
      minuteTimer.addEventListener(TimerEvent.TIMER, onTick);
      minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE,
      onTimerComplete);

      minuteTimer.start();
      }

      public function onTick(evt:TimerEvent):void {
      var minuteTimer:Timer = evt.target as Timer;
      var lvSecondsRemaining:int = minuteTimer.repeatCount -
      minuteTimer.currentCount;
      var lvMinutes:int = lvSecondsRemaining / 60;
      var lvSeconds:int = lvSecondsRemaining - ( lvMinutes * 60 );
      var lvSecondsText:String = lvSeconds.toString();
      if( lvSeconds < 10 ) lvSecondsText = "0" + lvSecondsText;

      lblTimeUntil.text = lvMinutes.toString() + ":" + lvSecondsText;

      }

      public function onTimerComplete(evt:TimerEvent):void{
      var minuteTimer:Timer = evt.target as Timer;
      minuteTimer.reset();
      minuteTimer.start();
      tempXML.send();

      }


      private function stopTimer():void{
      var minuteTimer:Timer = new Timer(1000, 300);
      minuteTimer.reset();
      minuteTimer.stop();
      clearFormHandler();

      }

      Here is the logout button and the login button
      Code:

      <mx:Button label="Log Out" height="27" fillAlphas="[1.0, 1.0]"
      fillColors="[#030000, #030000, #474545, #474545]"
      borderColor="#DCDEDF" click="currentState=''; stopTimer();"/>


      <mx:Button x="10" y="282" label="Login" id="Submit"
      click="login_user.send();"
      fontSize="14" themeColor="#FFA800" borderColor="#FA0202"
      enabled="{formIsValid}" />

      If the user is able to login
      Code:

      currentState = "Logged In";

      tempXML.send();
      getDaysUntil();

      Any ideas how to get this to display right
        • 1. Re: clock killer
          m_hartnett Level 3
          At some point you need to remove the eventListener for the timer.
          Not removing the listeners causes the Flex garbage collection not to remove it.

          I believe that if you issue
          minuteTimer.removeEventListener(TimerEvent.TIMER, onTick);
          minuteTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,
          onTimerComplete);

          in one of your methods then the garbage collector will destroy it and only your new timer will have the listener events.
          • 2. Re: clock killer
            m_hartnett Level 3
            At some point you need to remove the eventListener for the timer.
            Not removing the listeners causes the Flex garbage collection not to remove it.

            I believe that if you issue
            minuteTimer.removeEventListener(TimerEvent.TIMER, onTick);
            minuteTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,
            onTimerComplete);

            in one of your methods then the garbage collector will destroy it and only your new timer will have the listener events.