How to Make a Day Countdown Timer in Flash CS4
A countdown timer provides an update at a glance on the number of days remaining before a certain event. Adobe Flash CS4 includes the ActionScript programming language, which gives you a variety of ways to create your own custom functions. Use Flash CS4 to create a countdown timer that uses ActionScript to update the day.
Instructions
-
-
1
Launch Flash. Click “File” and select “New” from the context menu. Choose “Flash File (ActionScript 3.0)” and click “OK.”
-
2
Click the “Text” tool from the Tools panel and drag on the screen to create a text box. Set the “Text Type” of the text box to “Dynamic Text” from the Properties panel. Set the “Instance Name” to “timer_display.”
-
-
3
Click the first frame of the timeline and press “F9.” Type the following code into the Actions window that opens:
// register function
addEventListener('enterFrame',daytimer_handler);// calls repeatedly
function daytimer_handler(evt:Event):void{
// current date
var today:Date = new Date();
// current Year
var currentYear = today.getFullYear();
// current month
var currentMonth = today.getMonth();
// current day
var currentDay = today.getDate();// current time
var currentTime = today.getTime();
// target date (5 days from now change to your need
var targetDate:Date = new Date(currentYear, currentMonth, currentDay+5);
var targetDay = targetDate.getTime();
// time remaining
var timeLeft = targetDay-currentTime;
var sec = Math.floor(timeLeft/1000);
var min = Math.floor(sec/60);
var hours = Math.floor(min/60);
var days = Math.floor(hours/24);// convert sec to string
sec = String(sec%60);// if less than add a 0
if (sec.length<2) {
sec = "0"+sec;
}min = String(min%60);
if (min.length<2) {
min = "0"+min;
}hours = String(hours%24);
if (hours.length<2) {
hours = "0"+hours;
}days = String(days);
if (timeLeft>0) {
// display day string
var dayCounter:String = days;
timer_display.text = dayCounter;
} else {
trace("Happy Birthday!");
var newTime:String = "0";
timer_display.text = newTime;
removeEventListener('enterFrame',daytimer_handler);
}};
-
1
Resources
- Photo Credit Burke/Triolo Productions/Brand X Pictures/Getty Images