Skip to main content
Inspiring
April 24, 2013
Answered

How to reset my Timer

  • April 24, 2013
  • 1 reply
  • 1212 views

Hello!

I have a question and I hope I can find a solution here.I have a short quiz and I want to allow the user only 30 seconds per question to give a right answer.

I have a "start" button that triggers a countdown timer for the first time. Then, I want each time the user clicks  the button "next", the countdown to start again counting from 30 to 0. This is what I have tried so far:

var fl_SecondsToCountDown:Number = 30;

var fl_CountDownTimerInstance:Timer = new Timer(1000, fl_SecondsToCountDown);

fl_CountDownTimerInstance.addEventListener(TimerEvent.TIMER, fl_CountDownTimerHandler);

start_btn.addEventListener(MouseEvent.CLICK,startCountdown);

next_btn.addEventListener(MouseEvent.CLICK, goNext);

function fl_CountDownTimerHandler(event:TimerEvent):void

{

          seconds_txt.text = String(fl_SecondsToCountDown);

          fl_SecondsToCountDown--;

}

function startCountdown(e:MouseEvent):void{

          fl_CountDownTimerInstance.start();

}

function goNext(e:MouseEvent):void{

                    fl_CountDownTimerInstance.reset();

}

Now, what it does it's just stopping the countdown at 25 seconds, for example, and then if I ask it to start again, it starts where it has left off, instead of going back to 30 seconds again.

Thanks a lot!

This topic has been closed for replies.
Correct answer kglad

fl_SecondsToCountDown helps me decrement the number of seconds. I don't know another alternative to that...

function fl_CountDownTimerHandler(event:TimerEvent):void

{

          seconds_txt.text = String(fl_SecondsToCountDown);

         fl_SecondsToCountDown--;

}


use:

function fl_CountDownTimerHandler(event:TimerEvent):void

{

          seconds_txt.text = String(fl_CountDownTimerInstance.repeatCount-fl_CountDownTimerInstance.currentCount);

       

}

1 reply

kglad
Community Expert
Community Expert
April 24, 2013

use the reset() method:

fl_CountDownTimerInstance.reset();

// then apply the start() method when you want your timer to (re)start

Raloo6Author
Inspiring
April 24, 2013

I have tried that now. I have put fl_CountDownTimerInstance.reset(); in a function, and then when the user clicks "Next" I have put fl_CountDownTimerInstance.start();

But what it does is simply stopping the countdown at a certain point, and then when I click "Next", it continues from where it has left off. It's like it just pauses.

kglad
Community Expert
Community Expert
April 24, 2013

no, it resets the timer so it stops after 30 more calls.

it does not reset fl_SecondsToCountDown to 30, though.  in fact, you should NOT be re-defining that unless that serves some purpose.

you should be checking the timer's currentCount to see how many calls have been made and how many are remaining, if that's what you are trying to access by using fl_SecondsToCountDown.