Copy link to clipboard
Copied
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!
use:
function fl_CountDownTimerHandler(event:TimerEvent):void
{
seconds_txt.text = String(fl_CountDownTimerInstance.repeatCount-fl_CountDownTimerInstance.currentCount);
}
Copy link to clipboard
Copied
use the reset() method:
fl_CountDownTimerInstance.reset();
// then apply the start() method when you want your timer to (re)start
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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--;
}
Copy link to clipboard
Copied
use:
function fl_CountDownTimerHandler(event:TimerEvent):void
{
seconds_txt.text = String(fl_CountDownTimerInstance.repeatCount-fl_CountDownTimerInstance.currentCount);
}
Copy link to clipboard
Copied
Yes it worked! Thank you very very much for your help!
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now