View Full Version : Adding milisseconds to a countdown
Shuugo
May 29th, 2008, 09:45 PM
Hello. I used to come to Kirupa before O.o but never realized it had a forum. The thing is: I need help adding "milisseconds" to a countdown where I currently have "days", "hours", "minutes" and "seconds". The intention here is to remove the "days" and add "milisseconds". I got the (flawed) code from a tutorial, fixed it here and there, and somehow it works now, but I am a newcomer to Flash and I understand absolutely nothing of ActionScript, so adding the milisseconds is way beyond what I can do now >_<.
Here's the code:
this.onEnterFrame = function() {
var today:Date = new Date();
var currentYear = today.getFullYear();
var currentTime = today.getTime();
var targetDate:Date = new Date(currentYear,5,1);
var targetTime = targetDate.getTime();
var timeLeft = targetTime - currentTime;
var sec = Math.floor (timeLeft/1000);
var min = Math.floor (sec/60);
var hrs = Math.floor (min/60);
var days = Math.floor (hrs/24);
sec = string(sec % 60);
if (sec.length < 2) {
sec = "0" + sec;
}
min = string(min % 60)
if (min.length < 2) {
min = "0" + min;
}
hrs = string(hrs % 24)
if (hrs.length < 2) {
hrs = "0" + hrs;
}
days = string(days);
var counter:String = days + ":" + hrs + ":" + min + ":" + sec;
countdown_txt.text = counter;
}
I really need help with this >_< and it is kind of urgent.
Thanks a LOT in advance to anyone who replies here, any help is appreciated.
rbnzdave
May 29th, 2008, 10:08 PM
here. ( well its modified to correctly get the milliseconds, however the function name will be invalid in as3.
this.onEnterFrame = function() {
var today:Date = new Date();
var currentYear = today.getFullYear();
var currentTime = today.getTime();
var targetDate:Date = new Date(currentYear,5,1);
var targetTime = targetDate.getTime();
var timeLeft = targetTime - currentTime;
var mSec = Math.floor (timeLeft);
var sec = Math.floor (timeLeft/1000);
var min = Math.floor (sec/60);
var hrs = Math.floor (min/60);
//var days = Math.floor (hrs/24);
mSec = String(mSec %1000);
if (mSec.length < 2) {
mSec = "00" + sec;
} else if (mSec.length < 3) {
mSec = "0" + sec;
}
sec = String(sec % 60);
if (sec.length < 2) {
sec = "0" + sec;
}
min = String(min % 60)
if (min.length < 2) {
min = "0" + min;
}
hrs = String(hrs % 24)
if (hrs.length < 2) {
hrs = "0" + hrs;
}
var counter:String = hrs + ":" + min + ":" + sec + ":" + mSec;
countdown_txt.text = counter;
}
Shuugo
May 29th, 2008, 11:06 PM
Thanks a lot. It's getting the milisseconds now, however, some problems arised:
1. The counter quickly corrects the value to a base 60 number, but you can see it making the correction (it fastly displays the numbers 9,8,7 and then slows down and keeps counting)
2. The milisseconds counter has 3 numbers, I need it to have 2 (forgot to mention it, my apologies >_<)
3. It's not getting the time correctly @_@
Can anyone please provide me help with those new issues? Thanks a lot for replying and helping me, rbnzdave ^^
rbnzdave
May 29th, 2008, 11:28 PM
time is correct for me. 31 days this month. days:1 hrs:8 mins:33 sec:12 msec:14
have changed the resolution on the mSec ( added days to check the time reading )
var today:Date = new Date();
var currentYear = today.getFullYear();
var currentTime = today.getTime();
var targetDate:Date = new Date(currentYear,5,1);
var targetTime = targetDate.getTime();
var timeLeft = targetTime - currentTime;
var mSec = Math.floor (timeLeft);
var sec = Math.floor (timeLeft/1000);
var min = Math.floor (sec/60);
var hrs = Math.floor (min/60);
var days = Math.floor (hrs/24);
mSec = String(mSec %100);
if (mSec.length < 2) {
mSec = "0" + sec;
}
sec = String(sec % 60);
if (sec.length < 2) {
sec = "0" + sec;
}
min = String(min % 60)
if (min.length < 2) {
min = "0" + min;
}
hrs = String(hrs % 24)
if (hrs.length < 2) {
hrs = "0" + hrs;
}
days = String(days);
var counter:String = days + ":" + hrs + ":" + min + ":" + sec + ":" + mSec;
trace(counter);
rbnzdave
May 29th, 2008, 11:28 PM
oh by the way, you do know your script counts down to the 1st June....
Shuugo
May 31st, 2008, 04:57 PM
With those new changes, now the script does not count >_<. In my output window, I get a value which I don't really understand (might be the time left for the day to come), but the counting doesn't start, I just get an empty box.
I'll post a screenshot: http://img135.imageshack.us/img135/1749/countgx0.jpg
Sorry for not replying earlier >_< I had a busy day yesterday, didn't even manage to get on the PC >_<
If possible, keep helping me >_< I have to get this done for tomorrow night at most T___T and I'd like it to be perfect, which is near to happen thanks to the help I've been getting. Thanks a lot and keep it up, please ^^!
Shuugo
May 31st, 2008, 05:10 PM
Oh, on some other forum, people gave me this code, which looks a lot like the one rbnzdave gave me before, but it might be useful, I don't know >< (I've made little adjustments since it was crashing for some reason @_@):
this.onEnterFrame = function() {
var today:Date = new Date();
var currentYear = today.getFullYear();
var currentTime = today.getTime();
var targetDate:Date = new Date(currentYear, 5, 1);
var targetTime = targetDate.getTime();
var timeLeft = targetTime-currentTime;
var mili = (timeLeft);
var sec = Math.floor(timeLeft/1000);
var min = Math.floor(sec/60);
var hrs = Math.floor(min/60);
sec = (sec % 60);
mili = (mili % 600);
if (sec.length < 2) {
sec = "0"+sec;
}
min = (min%60);
if (min.length < 2) {
min = "0"+min;
}
hrs = (hrs%24);
if (hrs.length < 2) {
hrs = "0"+hrs;
}
var counter:String = hrs+":"+min+":"+sec+":"+mili;
countdown_txt.text = counter;
};
It works, it counts everything, BUT, if the hours value is not 10,11 or 12, it displays as a single number, like "X", instead of "0X". I wanted the decimal for the hours value to always be displayed >_<. Also, the milisseconds has 3 numbers, instead of 2, which is the goal here.
Just decided to leave this code here for anyone to see because it might help solving the problems somehow @_@. Also, this one does not have days, so I assume the time checking is wrong (tested it and it seemed wrong indeed)
And answering what rbnzdave asked before, yes, I know about the 0-11 things on the month, so 5 = June indeed ^^ but thanks for warning me.
Well >_< I'm really depending on you guys help ^^ so please, lend me a hand on this one if it's in your power >_< there's no way I can accomplish this by myself.
Thanks a lot in advance ^^.
Shuugo
June 1st, 2008, 04:06 AM
I'm running out of time T_T someone please, save my day~
Shuugo
June 1st, 2008, 11:45 PM
Solved it with a different way 8D thanks for the help ^^!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.