3

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?

hitek
  • 372
  • 1
  • 17
  • 33

4 Answers4

4

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);
Jonathan Graef
  • 391
  • 3
  • 12
4

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);
Brian
  • 752
  • 6
  • 10
  • do you take in account date in this example, what if somebody starts the quiz 1 minute before midnight ? – Omu Nov 01 '09 at 19:51
  • it makes no difference, the difference in milliseconds between 11pm and 1 am should be the same as between 11am and 1 pm – john Smith Nov 17 '13 at 17:27
1

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();
mica
  • 500
  • 3
  • 13
0
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);
}