Copy link to clipboard
Copied
I need a timer to wait 5 seconds after a button is clicked before displaying something else. I am having trouble with this and I am sure its something I am doing wrong. It seems to work but doesnt stop.
Here is what I have so far any help would be appreciated.
function tortTimer()
{
trace("Tort Timer");
var tortInterval:Timer = new Timer(750);
tortInterval.addEventListener(TimerEvent.TIMER, tortProcessing);
tortInterval.start();
}
function tortProcessing (e:TimerEvent):void {
if (rotationsPM < 20)
{
trace("Not long enough");
feeback_MC.visible = true;
tort_wrong.visible = true;
next_btn.visible = true;
} else if ( rotationsPM > 25) {
trace("Its overcooked");
feeback_MC.visible = true;
tort_wrong.visible = true;
next_btn.visible = true;
} else if (rotationsPM >=20 || rotationsPM <= 25) {
trace("Just Right");
feeback_MC.visible = true;
tort_right.visible = true;
next_btn.visible = true;
tortInterval.removeEventListener(TimerEvent.TIMER, tortProcessing);
}
}
If you want the Timer to stop after a certain number of timeouts, even just 1, you need to specify that as its second argument.
If you want the second function to be able to target the Timer instance to remove its listener you need to declare the Timer outside of the first function so that it is available to other functions.
Copy link to clipboard
Copied
If you want the Timer to stop after a certain number of timeouts, even just 1, you need to specify that as its second argument.
If you want the second function to be able to target the Timer instance to remove its listener you need to declare the Timer outside of the first function so that it is available to other functions.
Copy link to clipboard
Copied
Thanks. I knew it was something simple.
Copy link to clipboard
Copied
You're welcome
Copy link to clipboard
Copied
That's not technically true (though it is best bractice).
You can get a reference to the timer like this:
function tortProcessing (e:TimerEvent):void {
var timer:Timer = e.currentTarget as Timer;
...
}
The real concern about not declaring the timer outside of tortProcessing is that the timer may well get garbage collected before the listener fires.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now