• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

help in AS3 code of 24 hour countdown timer

Guest
Jan 02, 2012 Jan 02, 2012

Copy link to clipboard

Copied

hello,

i  just wanna ask if someone can help me,

i created a 24 hour flash countdown timer here http://allofmyworks.weebly.com/flash.html

the problem is when it reaches the desired time, the time still counts and became negative,

what i want is to make it only 24hour countdown clock and when it reaches the time it will only stay in 00:00:00

thanks

here is the code i used

var endDate:Date = new Date(2012,0,4);

var countdownTimer:Timer = new Timer(1000);

countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);

countdownTimer.start();

function updateTime(e:TimerEvent):void

{

          var now:Date = new Date();

          var timeLeft:Number = endDate.getTime() - now.getTime();

          var seconds:Number = Math.floor(timeLeft / 1000);

          var minutes:Number = Math.floor(seconds / 60);

          var hours:Number = Math.floor(minutes / 60);

 

 

          seconds %= 60;

          minutes %= 60;

 

 

          var sec:String = seconds.toString();

          var min:String = minutes.toString();

          var hrs:String = hours.toString();

 

 

          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;

}

TOPICS
ActionScript

Views

6.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 03, 2012 Jan 03, 2012

to end on that date at 0800, use:

var endDate:Date = new Date(2012,0,4,8);

to use a 24hr countdown:

var endDate:Date = new Date(new Date().getTime()+24*60*60*1000);

Votes

Translate

Translate
Community Expert ,
Jan 02, 2012 Jan 02, 2012

Copy link to clipboard

Copied

:

var endDate:Date = new Date(2012,0,4);

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 seconds:Number = Math.floor(timeLeft / 1000);

          var minutes:Number = Math.floor(seconds / 60);

          var hours:Number = Math.floor(minutes / 60);

 

 

          seconds %= 60;

          minutes %= 60;

 

 

          var sec:String = seconds.toString();

          var min:String = minutes.toString();

          var hrs:String = hours.toString();

 

 

          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;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 02, 2012 Jan 02, 2012

Copy link to clipboard

Copied

thanks brother, can you make it specific time on this part

var endDate:Date = new Date(2012,0,4);

let say i want it to end at that date above but on specific time like 8 in the morning

thanks Dr. Gladstien

another is can you make another code which is just 24 hour countdown, no specific date like the above because i will use it frequently and i need to change the date on the code everytime right?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2012 Jan 03, 2012

Copy link to clipboard

Copied

to end on that date at 0800, use:

var endDate:Date = new Date(2012,0,4,8);

to use a 24hr countdown:

var endDate:Date = new Date(new Date().getTime()+24*60*60*1000);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 03, 2012 Jan 03, 2012

Copy link to clipboard

Copied

thank you very much for helping Dr.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2012 Jan 03, 2012

Copy link to clipboard

Copied

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 05, 2012 Jan 05, 2012

Copy link to clipboard

Copied

sir another question

on this code

to use a 24hr countdown:

var endDate:Date = new Date(new Date().getTime()+24*60*60*1000);

when it reaches 00:00:00 it does not stop there but instead the 24 hour countdown will start again

can you please revised the code so it will stop on 00:00:00

thank you very much

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2012 Jan 05, 2012

Copy link to clipboard

Copied

copy and paste your code.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 05, 2012 Jan 05, 2012

Copy link to clipboard

Copied

here is the code

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();

          var timeLeft:Number = endDate.getTime() - now.getTime();

          var seconds:Number = Math.floor(timeLeft / 1000);

          var minutes:Number = Math.floor(seconds / 60);

          var hours:Number = Math.floor(minutes / 60);

          seconds %= 60;

          minutes %= 60;

          var sec:String = seconds.toString();

          var min:String = minutes.toString();

          var hrs:String = hours.toString();

          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;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 05, 2012 Jan 05, 2012

Copy link to clipboard

Copied

use the code i suggested:

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 seconds:Number = Math.floor(timeLeft / 1000);

          var minutes:Number = Math.floor(seconds / 60);

          var hours:Number = Math.floor(minutes / 60);

 

 

          seconds %= 60;

          minutes %= 60;

 

 

          var sec:String = seconds.toString();

          var min:String = minutes.toString();

          var hrs:String = hours.toString();

 

 

          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;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 07, 2012 Jan 07, 2012

Copy link to clipboard

Copied

Dr. on the last code you sent, the countdown restarts from the start everytime the page was refresh

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 07, 2012 Jan 07, 2012

Copy link to clipboard

Copied

it starts 1 day in advance of the current day.  ie, it's a 24 hour countdown.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 09, 2012 Jan 09, 2012

Copy link to clipboard

Copied

thanks doctor

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2012 Jan 09, 2012

Copy link to clipboard

Copied

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 20, 2012 Jun 20, 2012

Copy link to clipboard

Copied

Hello Doctor,

How to make it Recurring ?

if(time_txt.text == "00:00:00")

{

countdownTimer.reset();

countdownTimer.start();

}

Thank You.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 21, 2012 Jun 21, 2012

Copy link to clipboard

Copied

:

var endDate:Date;

var countdownTimer:Timer = new Timer(1000);

countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);

startF();

function startF():void{

new Date(new Date().getTime()+24*60*60*1000);

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();

// if you want to restart:

startF();

return

}

          var timeLeft:Number = endDate.getTime() - now.getTime();

          var seconds:Number = Math.floor(timeLeft / 1000);

          var minutes:Number = Math.floor(seconds / 60);

          var hours:Number = Math.floor(minutes / 60);

 

 

          seconds %= 60;

          minutes %= 60;

 

 

          var sec:String = seconds.toString();

          var min:String = minutes.toString();

          var hrs:String = hours.toString();

 

 

          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;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 21, 2012 Jun 21, 2012

Copy link to clipboard

Copied

Hello Doctor,

Thank you for your prompt reply

But I get this error:

"

TypeError: Error #1009: Cannot access a property or method of a null object reference.

    at Untitled_fla::MainTimeline/updateTime()

    at flash.utils::Timer/_timerDispatch()

    at flash.utils::Timer/tick()

"

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 21, 2012 Jun 21, 2012

Copy link to clipboard

Copied

function startF():void{

new Date(new Date().getTime()+24*60*60*1000);

countdownTimer.start();

}

should be

function startF():void{

endDate=new Date(new Date().getTime()+24*60*60*1000);

countdownTimer.start();

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 22, 2012 Jun 22, 2012

Copy link to clipboard

Copied

Thank You Doctor,

You Truly Are Inspirational.

A Vital Part of This Community.

Thanks Again.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 22, 2012 Jun 22, 2012

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines