Question

I am building a quiz and i need to calculate the total time taken to do the quiz. and i need to display the time taken in HH::MM::SS..any pointers?

Was it helpful?

Solution

new Date().time returns the time in milliseconds.

var nStart:Number = new Date().time;

// Some time passes

var nMillisElapsed:Number = new Date().time - nStart;

var strTime:String = Math.floor(nMillisElapsed / (1000 * 60 * 60)) + "::" + 
   (Math.floor(nMillisElapsed / (1000 * 60)) % 60) + "::" + 
   (Math.floor(nMillisElapsed / (1000)) % 60);

OTHER TIPS

I resurrect this question to say that both Brian and mica are wrong. Creating a new Date() gives you the time according to the computer's clock. All someone has to do is set their clock back several minutes, and that would cause the quiz timer to go back several minutes as well. Or worse, they could set their clock back to a time before they started the quiz, and your app would think they spent a negative amount of time taking the quiz. o.O

The solution is to use flash.utils.getTimer(). It returns the number of milliseconds since the swf started playing, regardless of what the computer's clock says.

Here's an example:

var startTime:Number = getTimer();

// then after some time passes:

var elapsedMilliseconds:Number = getTimer() - startTime;

Then you can use Brian's code to format the time for display:

var strTime:String = Math.floor(elapsedMilliseconds / (1000 * 60 * 60)) + "::" + 
(Math.floor(elapsedMilliseconds / (1000 * 60)) % 60) + "::" + 
(Math.floor(elapsedMilliseconds / (1000)) % 60);

Fill with zero when number is less than 10 (Thanks brian)

var now:Date; //
var startDate:Date;
var startTime:Number; 
// initialize timer and start it
function initTimer():void{
    startDate = new Date();
    startTime = startDate.getTime();
    //
    var timer:Timer = new Timer(1000,0); // set a new break
        timer.addEventListener(TimerEvent.TIMER, onTimer); // add timer listener
    //
    function onTimer():void{
        now=new Date();
        var nowTime:Number = now.getTime();
        var diff:Number = nowTime-startTime;
        var strTime:String = Math.floor(diff / (1000 * 60 * 60)) + ":" + 
                            zeroFill(Math.floor(diff / (1000 * 60)) % 60) + ":" + 
                            zeroFill(Math.floor(diff / (1000)) % 60);
        // display where you want
        trace('time elapsed : ' + strTime);
    }
    // fill with zero when number is less than 10
    function zeroFill(myNumber:Number):String{
        var zeroFilledNumber:String=myNumber.toString();
        if(myNumber<10){
            zeroFilledNumber = '0'+zeroFilledNumber;
        }
        return zeroFilledNumber;
    }

    // start TIMER
    timer.start();

}
initTimer();
var countdown:Timer = new Timer(1000);
countdown.addEventListener(TimerEvent.TIMER, timerHandler);
countdown.start();

function timerHandler(e:TimerEvent):void
{           
    var minute = Math.floor(countdown.currentCount /  60);
    if(minute < 10)
        minute = '0'+minute;

    var second = countdown.currentCount % 60;
    if(second < 10)
        second = '0'+second;


    var timeElapsed = minute +':'+second;
    trace(timeElapsed);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top