Copy link to clipboard
Copied
Hi,
I'm a beginner at flash.
i just wanna ask about making 24hours countdown clock.
what i want is to make it only 24hour countdown clock and when it reaches the time it will start again.(every day)
and The start time is 2 o'clok(2 pm) every single day.(for the pacific time)
and I found this code from (http://forums.adobe.com/message/4116774 )
It's similar with what i want but even I tried, I can't change for mind...
and I want using hundredths too.
so here is the code.
Please help me,Thank you.
-----------------------------------------------
var endDate:Date = new Date(new Date().getTime()+24*60*60*1000);
var countdownTimer:Timer = new Timer(1000);
countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
countdownTimer.start();
function updateTime(e:TimerEvent):void
{
var now:Date = new Date();
if(now.getTime()>endDate.getTime()){
time_txt.text = "00:00:00";
countdownTimer.stop();
return
}
var timeLeft:Number = endDate.getTime() - now.getTime();
var hundredth:Number = Math.floor(timeLeft / 10);
var seconds:Number = Math.floor(hundredth / 1000);
var minutes:Number = Math.floor(seconds / 60);
var hours:Number = Math.floor(minutes / 60);
seconds %= 60;
minutes %= 60;
var fs:String = hundredth.toString();
var sec:String = seconds.toString();
var min:String = minutes.toString();
var hrs:String = hours.toString();
if (fs.length < 2) {
sec = "0" + fs;
}
if (sec.length < 2) {
sec = "0" + sec;
}
if (min.length < 2) {
min = "0" + min;
}
if (hrs.length < 2) {
hrs = "0" + hrs;
}
var time:String = hrs + ":" + min + ":" + sec;
time_txt.text = time;
}
if you want endDate to be today at 2pm, among other ways to do this, you can use:
var endDate:Date = new Date();
endDate.setHours(14);
endDate.setMinutes(0);
endDate.setSeconds(0);
Copy link to clipboard
Copied
if you want endDate to be today at 2pm, among other ways to do this, you can use:
var endDate:Date = new Date();
endDate.setHours(14);
endDate.setMinutes(0);
endDate.setSeconds(0);
Copy link to clipboard
Copied
wow. Thank U so much......
and can I ask one more thing?
How can I insert hundredth?
I wanna do like this
start_time = getTimer();
countdown = 7200000;
onEnterFrame = function () {
elapsed_time = getTimer()-start_time;
_root.count_down.text = time_to_string(_root.countdown-elapsed_time);
};
function time_to_string(time_to_convert) {
elapsed_hours = Math.floor(time_to_convert/3600000);
remaining = time_to_convert-(elapsed_hours*3600000);
elapsed_minutes = Math.floor(remaining/60000);
remaining = remaining-(elapsed_minutes*60000);
elapsed_seconds = Math.floor(remaining/1000);
remaining = remaining-(elapsed_seconds*1000);
elapsed_fs = Math.floor(remaining/10);
if (elapsed_hours<10) {
hours = "0"+elapsed_hours.toString();
} else {
hours = elapsed_hours.toString();
}
if (elapsed_minutes<10) {
minutes = "0"+elapsed_minutes.toString();
} else {
minutes = elapsed_minutes.toString();
}
if (elapsed_seconds<10) {
seconds = "0"+elapsed_seconds.toString();
} else {
seconds = elapsed_seconds.toString();
}
if (elapsed_fs<10) {
hundredths = "0"+elapsed_fs.toString();
} else {
hundredths = elapsed_fs.toString();
}
return hours+":"+minutes+":"+seconds+":"+hundredths;
}
Copy link to clipboard
Copied
you can use:
function updateTime(e:TimerEvent):void{
var now:Date = new Date();
if(now.getTime()>endDate.getTime()){
time_txt.text = "00:00:00";
countdownTimer.stop();
return
}
var timeLeft:Number = endDate.getTime() - now.getTime();
var hrs:int = Math.floor(timeLeft/(1000*60*60));
var min:int = (Math.floor(timeLeft/(1000*60) - hrs*60));
var sec:int = Math.floor(timeLeft/1000-hrs*60*60-min*60)%60;
var hun:int = Math.floor(timeLeft/10-hrs*100*60*60-min*100*60-sec*100);
var time:String = padF(hrs.toString()) + ":" + padF(min.toString())+ ":" + padF(sec.toString())+":"+padF(hun.toString());
time_txt.text = time;
}
function padF(s:String):String{
while(s.length<2){
s="0"+s;
}
return s
}
Copy link to clipboard
Copied
Thank U , and so sorry I have problem.,..
It Just stop and It was not start again..........What should I do?
because I wanna auto play every single day.
It should be end at 2pm,and then the countdown starts again for next day.
-----------------------------------------------------------------------
var endDate:Date = new Date();
endDate.setHours(14);
endDate.setMinutes(0);
endDate.setSeconds(0);
endDate.setMilliseconds(0);
var countdownTimer:Timer = new Timer(10);
countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
countdownTimer.start();
function updateTime(e:TimerEvent):void
{
var now:Date = new Date();
if(now.getTime()>endDate.getTime()){
time_txt.text = "00:00:00:00";
countdownTimer.stop();
return
}
var timeLeft:Number = endDate.getTime() - now.getTime();
var milliseconds:Number = Math.floor(timeLeft /10);
var seconds:Number = Math.floor(milliseconds / 100);
var minutes:Number = Math.floor(seconds / 60);
var hours:Number = Math.floor(minutes / 60);
milliseconds %= 100;
seconds %= 60;
minutes %= 60;
var mil:String = milliseconds.toString();
var sec:String = seconds.toString();
var min:String = minutes.toString();
var hrs:String = hours.toString();
if (mil.length < 2) {
mil = "0" + mil;
}
if (sec.length < 2) {
sec = "0" + sec;
}
if (min.length < 2) {
min = "0" + min;
}
if (hrs.length < 2) {
hrs = "0" + hrs;
}
var time:String = hrs + ":" + min + ":" + sec + ":" + mil;
time_txt.text = time;
}
Copy link to clipboard
Copied
that's from your part of the code:
if(now.getTime()>endDate.getTime()){
time_txt.text = "00:00:00";
countdownTimer.stop();
return
}
if you don't want to stop the countdown after 2pm, change endDate to the next day in that if-statement:
if(now.getTime()>endDate.getTime()){
endDate = new Date(endDate.getTime()+24*60*60*1000);
}
Copy link to clipboard
Copied
Try code below - it is much less verbose, takes less calculations and I think meets your other requirements except for pacific time:
stop();
var endDate:Date;
var countdownTimer:Timer;
init();
function init():void
{
initEndDate();
countdownTimer = new Timer(50);
countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
countdownTimer.start();
}
function initEndDate():void
{
endDate = new Date();
endDate.setDate(endDate.getUTCDate() + 1);
endDate.setHours(14);
endDate.setMinutes(0);
endDate.setSeconds(0);
endDate.setMilliseconds(0);
}
function updateTime(e:TimerEvent):void
{
var now:Date = new Date();
now = new Date(endDate.time - now.time);
if (now.time <= 0) {
initEndDate();
return;
}
time_txt.text = now.toUTCString().match(/(\d+:)+\d+/g) + ":" + String((now.millisecondsUTC < 10 ? "0" : "") + now.millisecondsUTC).match(/^\d\d/);
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now