• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1461
  • Last Modified:

Timing function for countdown timer

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.net.*;
import flash.display.*;
import flash.events.*;
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);
countdownTimer.start();
 
//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
{
	stage.nativeWindow.startMove();
}
 
//function to action when the close button listener is activated
function closeButton_CLICK(e:MouseEvent):void
{
	NativeApplication.nativeApplication.exit();
}
 
//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
	clearSettings();
	applyNewTimer();	
	//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

0
john_hollings
Asked:
john_hollings
  • 3
  • 2
1 Solution
 
blue-genieCommented:
I'll have a look if you upload your file (CS3)
i don't have time to replicate everything.
0
 
john_hollingsAuthor Commented:
Blue Genie,

Would really appreciate that thanks, please note this fla will be published as an AIR file.
Copy-of-Holiday-CountDown.zip
0
 
blue-genieCommented:
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 things.so 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.

0
 
john_hollingsAuthor Commented:
Hi,

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.
endDate.date=(myYear,myMonth,myDay);
 
endDate:Date = new Date(myYear,myMonth,myDay);
 
endDate.setDate(myYear,myMonth,myDay);

Open in new window

0
 
john_hollingsAuthor Commented:
Not to worry I figured it out I was using the wrong syntax

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

Thanks for your help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now