?
2010-03-20 20:24:07 UTC
I had to make a countdown timer in ActionScript 3 and I used this code:
//Create your Date() object
var endDate:Date = new Date(2010,3,5);
//Create your Timer object
//The time being set with milliseconds(1000 milliseconds = 1 second)
var countdownTimer:Timer = new Timer(1000);
//Adding an event listener to the timer object
countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
//Initializing timer object
countdownTimer.start();
//Calculate the time remaining as it is being updated
function updateTime(e:TimerEvent):void
{
//Current time
var now:Date = new Date();
var timeLeft:Number = endDate.getTime() - now.getTime();
//Converting the remaining time into seconds, minutes, hours, and days
var seconds:Number = Math.floor(timeLeft / 1000);
var minutes:Number = Math.floor(seconds / 60);
var hours:Number = Math.floor(minutes / 60);
var days:Number = Math.floor(hours / 24);
//Storing the remainder of this division problem
seconds %= 60;
minutes %= 60;
hours %= 24;
//Converting numerical values into strings so that
//we string all of these numbers together for the display
var sec:String = seconds.toString();
var min:String = minutes.toString();
var hrs:String = hours.toString();
var d:String = days.toString();
//Setting up a few restrictions for when the current time reaches a single digit
if (sec.length < 2) {
sec = "0" + sec;
}
if (min.length < 2) {
min = "0" + min;
}
if (hrs.length < 2) {
hrs = "0" + hrs;
}
//Stringing all of the numbers together for the display
var time:String = d + " DAYS " + hrs + " HOURS ";
//Setting the string to the display
time_txt.text = time;
}
It works fine, but it takes the local machine's time to calculate the time left and it changes in different timezones. How can I synchronize it with server's time for example?
Thanks in advance!