5 Replies Latest reply on Aug 4, 2010 8:11 AM by Ned Murphy

timer counting up in hours:minutes:seconds:milliseconds format

Good day,

Had a problem dealing with this kind of format.

And I want the milliseconds in 2 digits only.

Any help are greatly appreciated.

Here's my code:

intervalID = setInterval(this,"timerFunction",1);

function timerFunction(){

var msec:Number = getTimer();

if (msec <= 0){

play();

return;

}

var sec:Number = Math.floor(msec / 1000);

var min:Number = Math.floor(sec / 60);

var hour:Number = Math.floor(min / 60);

if(sec<10){

secStr="0"+String(sec);

}else{

secStr=sec;

}

if(min<10){

minStr="0"+String(min);

}else{

minStr=min;

}

if(hour<10){

hourStr="0"+String(hour);

}else{

hourStr=hour;

}

//trace("min: "+min);

//trace("sec: "+sec);

//trace("msec: "+msec);

_root.timerText = hourStr + ":" + minStr + ":" + secStr + ":" + msec;

}

Display result:

00:00:01:1012

00:00:02:2048

• 1. Re: timer counting up in hours:minutes:seconds:milliseconds format

There are a variety of issues with your approach that you need to work out.

If you want the milliseconds to show up as two digits only, that itself is a problem because there are 1000 of them in a second.  So you may want to clear up what you mean/intend there.  In practical terms, showing 2 digits would mean you are displaying the hundredths of a second, not msec.

What you are showing in your result are the total milliseconds that result from the getTimer() call.  You only need to display the number of them that are not going to be represented by the seconds (et al, as your seconds and minutes suffer the same issue).  And the number of them that represented are the remainder after you divide by 1000.

So if you have 63123 msecs as a result of the getTimer(), then you only want the 123 portion of that as your msec display.  The rest of it gets worked into the sec, min, and hour elements.

You can use the modulus operator to detemine what that remainder is using code...

msecToDisplay = msec%1000;

And you would have to do similar things for your seconds, and minutes.  As it is now, you will end up showing more than 60 seconds and more than 60 minutes in your display.

While someone might come along and hand you a solution to this, I think it will be a great learning experience for you if you try to work it out yourself.

• 2. Re: timer counting up in hours:minutes:seconds:milliseconds format

Thank you very much sir for the reply.

here is a link that give the idea of having milliseconds in 2 digit

http://www.catalent.com/offerings/drug-delivery/drug-delivery-oral/zydis-fast-dissolve-tec hnology

I hope I didn't violate on the rules by posting the link.

• 3. Re: timer counting up in hours:minutes:seconds:milliseconds format

You didn't violate any rules (don't know of too many except the ones about being polite, etc), but you neglected to say what to look for.  I did eventually find what I think you are talking about and what is displayed there makes no sense.  I think it is just a visual prop as it never seems to go above 20-something.  And seeing as how the times end up as exact values, it is definitely rigged.

As I mentioned already 1000 msec = 1 sec, and you can only get as high as 99 with two digits, so you need to think thru what you want to do.

• 4. Re: timer counting up in hours:minutes:seconds:milliseconds format

I got it work tnx.

• 5. Re: timer counting up in hours:minutes:seconds:milliseconds format

You're welcome