7 Replies Latest reply on Jan 27, 2012 8:00 AM by kglad

    How do I make my countdown stop at certain time of day?

    biker_babe06

      I followed a tutorial to create a countdown in Flash CS4 and it works except that I would like to have it countdown to a specific time of day. I will be updating something at 10pm each week and I would like my countdown to stop or countdown to 10pm on that day but I don't know how to make it do that. Is this possible? I haven't been able to find it anywhere. Below is the actionscript I am using.

      this.onEnterFrame = function() {

         

          var today:Date = new Date();

          var currentYear = today.getFullYear();

          var currentTime = today.getTime();

         

          var targetDate:Date = new Date(currentYear, 0, 27);

          var targetTime = targetDate.getTime();

         

          var timeLeft = targetTime - currentTime;

         

          var sec = Math.floor(timeLeft/1000);

          var min = Math.floor(sec/60);

          var hrs = Math.floor(min/60);

          var days = Math.floor(hrs/24);

          sec = String(sec%60);

          if (sec.length < 2) {

              sec = "0" + sec;

          }

          min = String(min%60);

          if (min.length < 2) {

              min = "0" + min;

          }

          hrs = String(hrs%24);

          if (hrs.length < 2) {

              hrs = "0" + hrs;

          }

          days = String(days);

         

          var counter:String = days + ":" + hrs + ":" + min + ":" + sec;

          time_txt.text = counter;

      }

       

      Thanks.

        • 1. Re: How do I make my countdown stop at certain time of day?
          kglad Adobe Community Professional & MVP

          var endDate:Date = new Date(use whatever here to specify your end date);

           

          clearInterval(updateTimeI);

          var updateTimeI=setInterval(updateTimeF,1000);

           

           

          function updateTime(e:TimerEvent):void

          {

                    var now:Date = new Date();

          if(now.getTime()>endDate.getTime()){

               time_txt.text = "00:00:00";

          clearInterval(updateTimeI);

          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;

          }

          • 2. Re: How do I make my countdown stop at certain time of day?
            biker_babe06 Level 1

            Thanks for the code, but something isn't working right. It gets this error: The class or interface 'TimerEvent' could not be loaded. Function updateTime(e:TimerEvent):void

            • 3. Re: How do I make my countdown stop at certain time of day?
              kglad Adobe Community Professional & MVP

              use:

               

               

               

               

              var endDate:Date = new Date(use whatever here to specify your end date);

               

              clearInterval(updateTimeI);

              var updateTimeI=setInterval(updateTimeF,1000);

               

               

              function updateTime():Void

              {

                        var now:Date = new Date();

              if(now.getTime()>endDate.getTime()){

                   time_txt.text = "00:00:00";

              clearInterval(updateTimeI);

              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;

              }

              1 person found this helpful
              • 4. Re: How do I make my countdown stop at certain time of day?
                biker_babe06 Level 1

                I don't think you are understanding my second question. I understand the fix has something to do with that bolded section "function updateTime():void"

                but now that is where the error is. Here is the error message: A type identifier is expected after the ':'.  What does that mean? I don't understand this code and

                so if you can figure out how to fix this that would be great.

                • 5. Re: How do I make my countdown stop at certain time of day?
                  kglad Adobe Community Professional & MVP

                  that should be:

                   

                  function updateTime():Void

                  • 6. Re: How do I make my countdown stop at certain time of day?
                    biker_babe06 Level 1

                    There is something missing from this function...It says there need to be something after the :

                    • 7. Re: How do I make my countdown stop at certain time of day?
                      kglad Adobe Community Professional & MVP

                      kglad wrote:

                       

                      use:

                       

                       

                       

                       

                      var endDate:Date = new Date(use whatever here to specify your end date);

                       

                      clearInterval(updateTimeI);

                      var updateTimeI=setInterval(updateTimeF,1000);

                       

                       

                      function updateTime():Void

                      {

                                var now:Date = new Date();

                      if(now.getTime()>endDate.getTime()){

                           time_txt.text = "00:00:00";

                      clearInterval(updateTimeI);

                      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;

                      }

                      1 person found this helpful