Timing function for countdown timer

Posted on 2009-04-28
Last Modified: 2013-11-11
Hi experts,

I am trying to create a desktop countdown timer (personal project) however I am getting some issues with the script.  The countdown timer works fine until I try and get user inputs.  I have created some variables  to store the end date and have a series of input fields for the user to provide their desired date.

I have provided the complete actionscript below in the hope this will help idenitfy the issue.  Basically when the user inputs their desired date and click OK the application takes these values converts them to a number and assigns them to the date variables.  Then it is supposed to update the countdowntimer start counting down from this new date value.

I have used trace to ensure the initial values are correct (which they are) and the ensure the user inputs are getting through and they are, I just can't seem to get the timer to refresh with the updated end date.

I appreciate any help provided.
// import flash common system classes

import flash.desktop.*;


import flash.display.*;


import flash.system.Capabilities;

import fl.transitions.*;

import fl.transitions.easing.*;

import flash.text.*;

import flash.display.Sprite;

// Create Variables and data type them to store application end Date

var myYear:Number;

var myMonth:Number;

var myDay:Number;

//Create user variables to store user inputs

var userYear:String;

var userMonth:String;

var userDay:String;

// Give application varibales initial values

myYear = 2009;

myMonth = 6;

myDay = 2;

// As Jan is referenced in AS as 0 -1 from the actual month number;

myMonth = myMonth -1;

//Assign a new Date variable with the target date initially set.

var endDate:Date = new Date(myYear,myMonth,myDay);

trace (endDate);

//create time with a delay of 1 second

var countdownTimer:Timer = new Timer(1000);

countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);


//Update timer function fired every second

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

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


	seconds %= 60;

	minutes %= 60;

	hours %= 24;


	var sec:String = seconds.toString();

	var min:String = minutes.toString();

	var hrs:String = hours.toString();

	var d:String = days.toString();


	if(sec.length < 2) {

		sec = "0" + sec;


	if(min.length < 2) {

		min = "0" + min;


	if(hrs.length < 2) {

		hrs = "0" + hrs;


	if(d.length == 2) {

		d = "0" + d;


	if(d.length == 1) {

		d = "00" + d;


	var time:String = d + ":" + hrs + ":" + min + ":" + sec;


	countDown_mc.time_txt.text = time;


//create event listener

background_mc.addEventListener(MouseEvent.MOUSE_DOWN, back_CLICK);

close_btn.addEventListener(MouseEvent.CLICK, closeButton_CLICK);

settings_btn.addEventListener(MouseEvent.CLICK, settingsbtn_CLICK);

settings_mc.ok_btn.addEventListener(MouseEvent.CLICK, okbtn_CLICK);

settings_mc.cancel_btn.addEventListener(MouseEvent.CLICK, cancelbtn_CLICK);

//***** ASSIGN FUNCTIONS ******

//function to action when back MC listener is activated

function back_CLICK(e:MouseEvent):void




//function to action when the close button listener is activated

function closeButton_CLICK(e:MouseEvent):void




//Set function for click event on settings button

function settingsbtn_CLICK(e:MouseEvent):void


	//Setup an alpha tween on the timer display

	var clockAlphaTween1:Tween = new Tween(countDown_mc, "alpha", Strong.easeOut, 1,0,2,true);

	//Listen for when the tween has finished and then run function

	clockAlphaTween1.addEventListener(TweenEvent.MOTION_FINISH, clockAlphaTween1_FINISH);


//Function for when alpha tween above is finished

function clockAlphaTween1_FINISH(e:TweenEvent):void


	//Setup an alpha tween for the settings mc

	var settingsAlphaTween1:Tween = new Tween(settings_mc, "alpha",Strong.easeOut, 0,1,2,true);

	//Setup a motion tween on the x axis for settings mc

	var settingsXTween1:Tween = new Tween(settings_mc, "x",Strong.easeOut, 416, 96, 2,true);


//Set function for click event for the cancel button

function cancelbtn_CLICK(e:MouseEvent):void


	//Setup an alpha tween on the settings mc

	var settingsAlphaTween2:Tween = new Tween(settings_mc, "alpha",Strong.easeOut, 1,0,2,true);

	//Setup a motion tween on the x axis for setting mc

	var settingsXTween2:Tween = new Tween(settings_mc, "x",Strong.easeOut, 96, 416, 2,true);

	//Listen for when the tween above has finished then run function

	settingsXTween2.addEventListener(TweenEvent.MOTION_FINISH, settingsXTween2_FINISH);


//Function for when motion tween has finished

function settingsXTween2_FINISH(e:TweenEvent):void


	//Setup alpha tween for timer display

	var clockAlphaTween2:Tween = new Tween(countDown_mc, "alpha", Strong.easeOut, 0,1,2,true);


//Set function for click event on ok button

function okbtn_CLICK(e:MouseEvent):void


	//Assign user inputed values to variables

	userYear = settings_mc.yearInput_txt.text;

	userMonth = settings_mc.monthInput_txt.text;

	userDay = settings_mc.dayInput_txt.text;


	//Convert string values to number data type and replace app variables with these values

	myYear = Number(userYear);

	myMonth = Number(userMonth)-1;

	myDay = Number(userDay);


	//Close the settings mc with alpha and motion tweens

	var settingsAlphaTween3:Tween = new Tween(settings_mc, "alpha",Strong.easeOut, 1,0,2,true);

	var settingsXTween3:Tween = new Tween(settings_mc, "x",Strong.easeOut, 96, 416, 2,true);

	//Listen for when motion tween has finished and run function

	settingsXTween3.addEventListener(TweenEvent.MOTION_FINISH, settingsXTween3_FINISH);


//Function for when motion tween has finished

function settingsXTween3_FINISH(e:TweenEvent):void


	//Run functions



	//Setup motion tween on x axis for timer display

	var clockAlphaTween3:Tween = new Tween(countDown_mc, "alpha", Strong.easeOut, 0,1,2,true);


//Function to clear the setting input fields

function clearSettings()


	settings_mc.yearInput_txt.text = "";

	settings_mc.monthInput_txt.text = "";

	settings_mc.dayInput_txt.text = "";


//Function to assign new endDate with new user values.

function applyNewTimer()


	var endDate:Date = new Date(myYear,myMonth,myDay);

	trace (endDate);


Open in new window

Question by:john_hollings
    LVL 39

    Expert Comment

    I'll have a look if you upload your file (CS3)
    i don't have time to replicate everything.
    LVL 5

    Author Comment

    Blue Genie,

    Would really appreciate that thanks, please note this fla will be published as an AIR file.
    LVL 39

    Accepted Solution

    oh hey sorry, your email alert must have slipped past during a manic session.
    had a quick look if you're still struggling with this.
    the problem is your endDate variable is not being updated because you're declaring the new variable in applyNewTimer which is then local , i.e. only persists within that function so it's not affecting the bigger scheme of to fix this you just need to declare the variable once at the top

    i.e var endDate:Date = new Date();

    and then when you use in later in all the other functions just use endDate and don't re-instantiate the variable.

    LVL 5

    Author Comment


    thanks for your response, I am still not getting this to work, if I have declared the endDate variable outside of the function (i.e. the top of my code) then all functions should be able to access and edit the data within this variable (is this correct?).

    If this is so how would I set the new date I have tried mutliple ways (see code snippet) but I keep getting various errors.  I hope you can help.,myMonth,myDay);
    endDate:Date = new Date(myYear,myMonth,myDay);

    Open in new window

    LVL 5

    Author Closing Comment

    Not to worry I figured it out I was using the wrong syntax

    endDate = new Date(myYear,myMonth,myDay);

    Thanks for your help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
    While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
    The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
    This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now