Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Timer Issue

Community Beginner ,
Oct 06, 2013 Oct 06, 2013

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);

                    }

}

TOPICS
ActionScript
847
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Oct 06, 2013 Oct 06, 2013

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.

Translate
LEGEND ,
Oct 06, 2013 Oct 06, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 06, 2013 Oct 06, 2013

Thanks. I knew it was something simple.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 06, 2013 Oct 06, 2013

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 07, 2013 Oct 07, 2013
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines