I've successfully managed to create a countdown timer with the help of a tutorial (I know nothing about actionscript), and everything is working as intended.
However, I need additional functions, and I don't know how to achieve this.
I need to have the timer start counting down to a NEW date when it reaches zero. Is this possible, and if so; how?
Please explain as if I'm 10 years old.
Here's the code:
Code:
//Create your Date() object
var endDate:Date = new Date(2009,1,13);
//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;
}
if (d.length < 2) {
d = "0" + d;
}
//Stringing all of the numbers together for the display
var time:String = d + ":" + hrs + ":" + min + ":" + sec;
//Setting the string to the display
time_txt.text = time;
if((d <= "00")&&(hrs <= "23")&&(min <= "59")&&(sec <= "59")){
countdownTimer.stop();
var newTime:String = "tomorr:ow";
time_txt.text = newTime;
}
else {
time = d + ":" + hrs + ":" + min + ":" + sec;
time_txt.text = time;
}
}