Skip to main content
rafa@mediatech
Inspiring
July 1, 2009
Answered

Timmer - timeout a section.

  • July 1, 2009
  • 1 reply
  • 1082 views

Hi everyone,

I designed a quiz application for client. It works fine, it keeps track of score, which questioned was missed, etc. Now my client would like me to make this quiz timed. He wants the user to only have a certain amount of time for finishing the quiz, if he doesnt finish it in the allocated time, he needs to start again.

I found some information on using Timers. I made a little test and it works fine. I am just not sure if this is the correct way to achieve what I am trying to accomplish. Also, how can I make a clock, so that the user can see how much time he has left, or how much time he has used?

This is a sample of the code I have so far. I just have the timer start, and then I have a box pop up after the time runs out:

screenBox.visible = false;


var mainTimer:Timer = new Timer(500,5);
mainTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (e:TimerEvent):void{
trace("Timer is Triggered");
}


mainTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
function timerDone(e:TimerEvent):void{
    trace("Timer finishing!");
    screenBox.visible = true;
}

mainTimer.start();

Thank you!!!

This topic has been closed for replies.
Correct answer kglad

Hi Kglad,

Thank you for your help yesterday. I think I am in the right path, but I ran into an issue. Maybe you can help me out. Here is my code so far:

screenBox.visible = false;

var secondsToCompleteQuiz:uint = 10;

var mainTimer:Timer = new Timer(1000,secondsToCompleteQuiz);

mainTimer.addEventListener(TimerEvent.TIMER, timerListener);

function timerListener (e:TimerEvent):void
{
     var timeRemaining:uint = secondsToCompleteQuiz-e.target.currentCount
     
     var seconds:Number = Math.floor(timeRemaining / 1000);
     var minutes:Number = Math.floor(seconds / 60);
     var hours:Number = Math.floor(minutes / 60);
     
     seconds %= 60;
     minutes %= 60;
     hours %= 24;
     
     var sec:String = seconds.toString();
     var min:String = minutes.toString();
     var hrs:String = hours.toString();
     
     if (sec.length < 2)

        {
          sec = "0" + sec;
     }
     
     if (min.length < 2)

        {
          min = "0" + min;
     }
     
     if (hrs.length < 2)

        {
          hrs = "0" + hrs;
     }
     
     var time:String = hrs + ":" + min + ":" + sec;
     
     tf.text = time;
}


mainTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
function timerDone(e:TimerEvent):void
{
    screenBox.visible = true;
}



mainTimer.start();

The timer is working. It is counting down and displaying my message after the time I allocate, BUT it is not displaying the time counting down! What am I doing wrong?

thank you,

Rafa.


use:

rafa@mediatech wrote:

Hi Kglad,

Thank you for your help yesterday. I think I am in the right path, but I ran into an issue. Maybe you can help me out. Here is my code so far:

screenBox.visible = false;

var secondsToCompleteQuiz:uint = 10;

var mainTimer:Timer = new Timer(1000,secondsToCompleteQuiz);

mainTimer.addEventListener(TimerEvent.TIMER, timerListener);

function timerListener (e:TimerEvent):void {
     var timeRemaining:uint = secondsToCompleteQuiz-e.target.currentCount
    
     var seconds:Number = timeRemaining;
     var minutes:Number = Math.floor(seconds / 60);
     var hours:Number = Math.floor(minutes / 60);
    
     seconds %= 60;
     minutes %= 60;
     hours %= 24;
    
     var sec:String = seconds.toString();
     var min:String = minutes.toString();
     var hrs:String = hours.toString();
    
     if (sec.length < 2)
        {           sec = "0" + sec;
     }           if (min.length < 2)
        {           min = "0" + min;
     }           if (hrs.length < 2)
        {           hrs = "0" + hrs;
     }           var time:String = hrs + ":" + min + ":" + sec;
    
     tf.text = time;
} mainTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone); function timerDone(e:TimerEvent):void {     screenBox.visible = true;
} mainTimer.start();


1 reply

kglad
Community Expert
Community Expert
July 1, 2009

screenBox.visible = false;

var secondsToCompleteQuiz:uint = 33;


var mainTimer:Timer = new Timer(1000,secondsToCompleteQuiz);

mainTimer.addEventListener(TimerEvent.TIMER, timerListener);


function timerListener (e:TimerEvent):void{

var timeRemaining:uint = 33-e.currentCount;
tf.text = timeRemaining.toString();  // create a textfield (tf) that will display the time remaining.

}


mainTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
function timerDone(e:TimerEvent):void{
    trace("Timer finishing!");

// inform the user that time has expired and do whatever to restart them.
    screenBox.visible = true;
}

mainTimer.start();

rafa@mediatech
Inspiring
July 1, 2009

Thanks Kglad,

I get an error on the following line:

var timeRemaining:uint = 33-e.currentCount;

the error says:

1119: Access of possibly undefined property currentCount through a reference with static type flash.events:TimerEvent.

It only gives me the error on publish... not when I "check syntax." What does it mean?

Rafa.

kglad
Community Expert
Community Expert
July 1, 2009

currentCount is a timer propery, not a timerevent property.  use:

e.target.currentCount