Skip to main content
vedtam
Known Participant
May 7, 2011
Answered

Simple count up timer won't stop.

  • May 7, 2011
  • 1 reply
  • 1023 views

Hi,

I made a simple count up timer, it is working just fine but I would like to have the %-sign next to the numbers so it would look as a preorder. But as I add "%" sign to the function, the counting never stops (at 100% any more), and continues counting.

What am I missing??

var count:Number = 1;
var myTimer:Timer = new Timer(10);

myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();

function countdown(event:TimerEvent):void{

//myText_txt.text = String((count)+myTimer.currentCount);

myText_txt.text = String((count)+myTimer.currentCount)+"%";


if (uint(myText_txt.text) >= 100) {
        myTimer.removeEventListener(TimerEvent.TIMER, countdown);
    }


}

Thanks!

This topic has been closed for replies.
Correct answer Ned Murphy

You should learn to use the trace() function to see why things don't work as expected.   The "%" makes converting it to a uint not what you expect.  Trace uint(myText_txt.text)  and you will see why comparing the uint value of a non-numeric string to a number isn't going to work.  Try the following instead...

var count:Number = 1;
var myTimer:Timer = new Timer(10);

myTimer.addEventListener(TimerEvent.TIMER, countdown);


myTimer.start();

function countdown(event:TimerEvent):void{

   myText_txt.text = String((count)+myTimer.currentCount)+"%";


   if (uint(String((count)+myTimer.currentCount)) >= 100) {
        myTimer.removeEventListener(TimerEvent.TIMER, countdown);
    }


}

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
May 7, 2011

You should learn to use the trace() function to see why things don't work as expected.   The "%" makes converting it to a uint not what you expect.  Trace uint(myText_txt.text)  and you will see why comparing the uint value of a non-numeric string to a number isn't going to work.  Try the following instead...

var count:Number = 1;
var myTimer:Timer = new Timer(10);

myTimer.addEventListener(TimerEvent.TIMER, countdown);


myTimer.start();

function countdown(event:TimerEvent):void{

   myText_txt.text = String((count)+myTimer.currentCount)+"%";


   if (uint(String((count)+myTimer.currentCount)) >= 100) {
        myTimer.removeEventListener(TimerEvent.TIMER, countdown);
    }


}

vedtam
vedtamAuthor
Known Participant
May 7, 2011

You nailed it again...hehe

I have traced:   trace (uint(myText_txt.text));  -and I got a lot of "0" in the output window, I will have to play a lot more with the trace function..

Thanks Ned!

Ned Murphy
Legend
May 7, 2011

You're welcome.  The trace function is one of the best tools for troubleshooting.