Skip to main content
trucatchtraps
Participant
November 4, 2015
Answered

How to restart a Countdown Timer?

  • November 4, 2015
  • 1 reply
  • 1295 views

Hi all!

I have a pretty simple countdown timer created to countdown to October 16th. Everything works fine except when the current date is between October 17th and December 31st. Once the current date is January 1st of the new year the timer will start again. How can I adjust my code in order to restart the Timer on October 17th. Basically, I am trying to get this countdown timer to work year round and only show 00:00:00:00 on October 16th, which I am planning on changing that to show some text instead (not yet started).

Below is the code I am currently using:

//onEnterFrame allows for a function to be called every tick

this.onEnterFrame = function()

{

  //Stores the current date

  var today:Date = new Date();

  //Stores the Current Year

  var currentYear = today.getFullYear();

  //Stores the Current Time

  var currentTime = today.getTime();

  //Creates and stores the target date

  var targetDate:Date = new Date(currentYear,9,16);

  var targetTime = targetDate.getTime();

  //Determines how much time is left.  Note: Leaves time in milliseconds

  var timeLeft = targetTime - currentTime;

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

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

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

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

  //Takes results of var remaining value.  Also converts "sec" into a string

  sec = String(sec % 60);

  //Once a string, you can check the values length and see whether it has been reduced below 2.

  //If so, add a "0" for visual purposes.

  if(sec.length < 2){

  sec = "0" + sec;

  }

  min = String(min % 60);

  if(min.length < 2){

  min = "0" + min;

  }

  hours = String(hours % 24);

  if(hours.length < 2){

   hours = "0" + hours;

  }

  days = String(days);

  if(timeLeft > 0 ){

   //Joins all values into one string value

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

   time_txt.text = counter;

  }

  else{

  trace("TIME'S UP");

        var newTime:String = "00:00:00:00";

        time_txt.text = newTime;

        delete (this.onEnterFrame);

  }

}

Thank you for any assistance!

This topic has been closed for replies.
Correct answer kglad

Thanks for this code, when I was reading through it made perfect sense and I thought for sure it was going to work! But unfortunately that still does not fix the issue. I am not getting any syntax errors, but it stays at 00:00:00:00 even with this added code.

  1. if(today.getMonth()>9 && today.getDate()>16){ 
  2.   currentYear++
  3.   } 
  4. var targetDate:Date = new Date(currentYear,9,16);

Both of your answers have been very helpful but neither have solved the problem. I feel I can only grant 1 answer as the correct answer.


my error.  that should be:

if(today.getMonth()>9 || (today.getMonth()==9 && today.getDate())>16){

currentYear++;

}

1 reply

kglad
Community Expert
Community Expert
November 5, 2015

if the current date is between 10/17 and 12/31, increment currentYear for your targetDate calculation.

trucatchtraps
Participant
November 5, 2015

Thank you for your prompt response. Okay I understand exactly what you are saying but I am not sure how to implement. This is my first project I have ever worked on in Flash Pro.

Is this what you are saying?

var targetDate:Date = new Date(currentYear,9,16);

  if(today > Date(currentYear,9,16)){

  currentYear = currentYear + 1;

  }

Or something like this:

var targetDate:Date = new Date(currentYear,9,16);

  if(today > targetDate){

  currentYear = currentYear + 1;

  }

Neither of these are the solution, but I am hoping you can point me in the right direction. I feel like I am on the right track, I am just not familiar with the coding of Flash and terminology.

Thank you again for the response

kglad
Community Expert
Community Expert
November 5, 2015

add this:

if(today.getMonth()>9 && today.getDate()>16){

currentYear++;

}

just before this:

var targetDate:Date = new Date(currentYear, 9, 16);

p.s when using the adobe forums, please mark helpful/correct responses, if there are any.