PDA

View Full Version : a countdown timer of only 15 minutes



tamccain
December 30th, 2004, 03:29 PM
I am trying to make a countdown timer of 15 minutes for a project I am doing. I have already done all the graphical work according to the tutorial on a date countdown timer, but I am not sure about how to code this so I can countdown for 15 minutes only. Any help would be greatly appreciated.

Thanks

arcaRox
December 31st, 2004, 11:14 AM
I am trying to make a countdown timer of 15 minutes for a project I am doing. I have already done all the graphical work according to the tutorial on a date countdown timer, but I am not sure about how to code this so I can countdown for 15 minutes only. Any help would be greatly appreciated.

Thanks

Here's a simple CountDownTimer class that you can use. It will countdown to the amount of time you specified and provide events for the progress and completion of the countdown. Then you create an instance of it, and do whatever display you wish to do in the event handlers.



// CountDownTimer class
function CountDownTimer (durationMin)
{
AsBroadcaster.initialize (this);
this.duration = durationMin * 60 * 1000; // duration in ms.
this.startTime = getTimer ();

this.id = setInterval (this, "checkTime", 1000);
this.checkTime ();
};

CountDownTimer.prototype.checkTime = function ()
{
if (getTimer () >= this.startTime + this.duration)
{
this.broadcastMessage ("onComplete");
clearInterval (this.id);
}
else
{
var timeLeft = this.duration - (getTimer () - this.startTime);
this.broadcastMessage ("onProgress", timeLeft);
};
};

// usage
// textfield used to display the timer.
createTextField ("timerTF", 0, 0, 0, 100, 100);

// create a 15 minutes countdown timer
myTimer = new CountDownTimer (15);
myTimer.addListener (this);


// handle time progress here
function onProgress (timeLeftMS)
{
var totalSeconds = timeLeftMS/1000;
var hours = Math.floor(totalSeconds / 3600);
var minutes = Math.floor((totalSeconds - ((hours > 0) ? 3600 : 0)) / 60);
var seconds = Math.floor(totalSeconds - ((hours * 3600) + (minutes * 60)));

if (hours < 10)
hours = "0" + hours;

if (minutes < 10)
minutes = "0" + minutes;

if (seconds < 10)
seconds = "0" + seconds;

timerTF.text = hours + ":" + minutes + ":" + seconds;
};

// handle countdown completion here
function onComplete ()
{
timerTF.text = "Done!";
};