0
votes

I'm building a flash desktop Quiz app, and i want to calculate how long it took the user to finish the quiz. so i did this to calculate the duration between when the user opens the first frame and when he arrives at the last frame:

//Code in the first Frame:
var startTime:Date = new Date();
var startMinutes:Number = startTime.getMinutes();
var startSeconds:Number = startTime.getSeconds();

and this is the code in the last frame:

//Code in the last Frame:
var endTime:Date = new Date();
var endMinutes:Number = endTime.getMinutes();
var endSeconds:Number = endTime.getSeconds();

var minutesDuration:Number = endMinutes - startMinutes;
var secondsDuration:Number = endSeconds - startSeconds;

durationTextField.text=String(minutesDuration)+":"+String(secondsDuration);

The problem is with the durationTextField, it doesn't display the duration, sometimes it displays a random number like 2 or 3 and a lot of times it displays nothing, why is that? is there any better way to tackle this whole duration problem than why i did?

1

1 Answers

1
votes

Based on this simple fonction and the getTimer() function you will get the exact time elasped between your first frame and your last frame.

on the first frame :

private function toTimeCode(milliseconds:int):String
{
    var seconds:int = Math.floor((milliseconds/1000) % 60);
    var strSeconds:String = (seconds < 10) ? ("0" + String(seconds)):String(seconds);
    var minutes:int = Math.round(Math.floor((milliseconds/1000)/60));
    var strMinutes:String = (minutes < 10) ? ("0" + String(minutes)):String(minutes);
    var strMilliseconds:String = milliseconds.toString();
    strMilliseconds = strMilliseconds.slice(strMilliseconds.length -3, strMilliseconds.length)
    var timeCode:String = strMinutes + ":" + strSeconds + ':' + strMilliseconds;
    return timeCode;
}

private var startTime:int = getTimer();

on the last frame :

private var endTime:int = getTimer();
durationTextField.text = toTimeCode(endTime - startTime);