Skip navigation
probag123
Currently Being Moderated

help in AS3 code of 24 hour countdown timer

Jan 2, 2012 8:57 PM

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;

}

 
Replies
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Jan 2, 2012 9:06 PM   in reply to probag123

    :

     

     

     

     

     

    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;

    }

     
    |
    Mark as:
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Jan 3, 2012 6:55 AM   in reply to probag123

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

     
    |
    Mark as:
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Jan 3, 2012 9:48 PM   in reply to probag123

    you're welcome.

     
    |
    Mark as:
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Jan 5, 2012 6:42 AM   in reply to probag123

    copy and paste your code.

     
    |
    Mark as:
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Jan 5, 2012 5:35 PM   in reply to probag123

    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;

    }

     
    |
    Mark as:
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Jan 7, 2012 8:07 AM   in reply to probag123

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

     
    |
    Mark as:
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Jan 9, 2012 8:18 AM   in reply to probag123

    you're welcome.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 20, 2012 11:39 PM   in reply to kglad

    Hello Doctor,

     

    How to make it Recurring ?

     

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

    {

    countdownTimer.reset();

    countdownTimer.start();

    }

     

    Thank You.

     
    |
    Mark as:
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 21, 2012 1:14 PM   in reply to kglad

    :

     

     

     

    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;

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 21, 2012 4:11 PM   in reply to kglad

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

    "

     
    |
    Mark as:
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 21, 2012 4:41 PM   in reply to KH000

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

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 22, 2012 1:01 AM   in reply to kglad

    Thank You Doctor,

     

    You Truly Are Inspirational.

     

    A Vital Part of This Community.

     

    Thanks Again.

     
    |
    Mark as:
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 22, 2012 6:02 AM   in reply to KH000

    you're welcome.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points