The Funk House

Welcome to The Funk House, home of Lee Doel a web designer and developer based in the UK.

slee — March 4, 2006, 5:50 pm

Countdown timer

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Ive had a request to make a countdown timer to a specific date with a sound to play each second, so here is a quick but of code that will do this for you. Ive tried to comment it so you can hopefully understand what is going on:


//*********************************************//
//*** countdown script with sound by Lee Doel *//
//*********************************************//
stop();
//edit this to set the month and day to count down too
// note the month needs to be one less so if its december you
// want put 11 as the numbers go from 0 - 11
month = "11";
day = "25";

//set up ticking sound
var tick:Sound = new Sound();
tick.attachSound("ticker");

// set up the date
currentDate = new Date();
thisYear = currentDate.getFullYear();

//day to countdown to
eventDate = new Date(thisYear, month, day);
eventMillisecs = eventDate.getTime();
createEmptyMovieClip("codeholder", 1);
codeholder.onEnterFrame = function() {

// get the current date and time as it exists at

currentDate = new Date();
currentMillisecs = currentDate.getTime();
this.msecs = eventMillisecs - currentMillisecs;

//do something once the countdown is complete
if (this.msecs < = 0){
delete this.onEnterFrame;
clearInterval(timer);
play();
return;
}

this.secs = Math.floor(this.msecs/1000); // 1000 milliseconds make a second
this.mins = Math.floor(this.secs/60); // 60 seconds make a minute
this.hours = Math.floor(this.mins/60); // 60 minutes make a hour
this.days = Math.floor(this.hours/24); // 24 hours make a second

this.msecs = string(this.msecs % 1000);
this.secs = string(this.secs % 60);
this.mins = string(this.mins % 60);
this.hours = string(this.hours % 24);
this.days = string(this.days);

//this next bit adds a 0 in front of the number to make it look better.
while (this.msecs.length < 3) this.msecs = "0" + this.msecs;
if (this.secs.length < 2) this.secs = "0" + this.secs;
if (this.mins.length < 2) this.mins = "0" + this.mins;
if (this.hours.length < 2) this.hours = "0" + this.hours;
while (this.days.length < 3) this.days = "0" + this.days;

this.cd = this.days + "." + this.hours + "." + this.mins + "." + this.secs;
//this puts a text box on the stage
//you might want to take this bit out and put your own on the stage.
this.createTextField("counter_txt", 1, 10, 10, 300, 100);
// finally, display values.
this.counter_txt.text = this.cd;
}
//play the sound every second
ticker = function () {
tick.start();
}
timer = setInterval(ticker, 1000);

all you need to do to get this to work is start a new movie and add this into the first frame.
then make a second keyframe and put what ever you want on it as this will display once the countdown is complete.
For the sound you will need to import a sound and right click the file in the library to edit the linkage to be exported for actionscript and change teh name to be ticker.

here is the fla (flash mx2004)

Please leave any comments :)

del.icio.us:Countdown timer digg:Countdown timer spurl:Countdown timer wists:Countdown timer reddit:Countdown timer fark:Countdown timer Y!:Countdown timer

No Comments »

RSS feed for comments on this post. TrackBack URI.

No comments yet.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>