Timers
You create a new Timer by creating a new Timer object. The constructor takes two parameters: the number of milliseconds to run and the number of events to cause.
One use of a timer is to create a clock. This Timer would run forever and would generate an event every second.
Creating a new timer
The DateCode:var clockTimer:Timer = new Timer(1000); // this timer generates an event every one second var timer2:Timer = new Timer(2000,2); // this timer generates an every every 2000 seconds. It only generates 2 events before stopping.
To get the current date, we create a date object and use methods like getHours(), getMinutes, getSeconds() and other methods.
This code gets the time in the format h:mm:ss
Example output:Code:var systemDate:Date = new Date(); var minutes:int = systemDate.getMinutes(); var hours:int = systemDate.getHours(); var seconds:int = systemDate.getSeconds(); var sSuffix:String; var sMinutes:String; var sSeconds:String; if (hours > 12) { // time is in 24 hour time, so subtracting 12 // puts it in 12 hour time hours -= 12; sSuffix = "P.M."; } else { sSuffix = "A.M."; } if (minutes < 10) { sMinutes = "0" + String(minutes); } else { sMinutes = String(minutes); } if (seconds < 10) { sSeconds = "0" + String(seconds); } else { sSeconds = String(seconds); } trace("The current time is " + String(hours) + ":" + sMinutes + ":" + String(sSeconds) + " " + sSuffix);
The current time is 1:06:59 P.M.
Notice, how I subtract 12 from the hours? This is because time is given in 24 hour time. If we want to display it in 12 hour time, we subtract 12 hours and append a suffix to the end.
Now instead of tracing the command to the output, we can display the time in a label. Then all we need to do is put that code in a callback function for a timer and we will have ourselves a live clock in action script.
So, add a label to the stage and change it's name to lblTime. Feel free to change the formatting of the label to anything you want.
Creating a new timer:
The time changes every second. So we set the interval to every 1000 milli seconds. This timer generates events forever.Code:var clockTimer:Timer = new Timer(1000);
Now we to add an event listener so we can call a function everytime the timer generates an event.
The updateTime function is simply going to contain the code from above. However, instead of showing text in the output window we are going to update the label.Code:clockTimer.addEventListener(TimerEvent.TIMER,updateTime);
Here is the function:
Now, all we have to do is start the timer outside of the function.Code:function updateTime(e:TimerEvent):void { var systemDate:Date = new Date(); var minutes:int = systemDate.getMinutes(); var hours:int = systemDate.getHours(); var seconds:int = systemDate.getSeconds(); var sPrefix:String; var sMinutes:String; var sSeconds:String; if (hours > 12) { // time is in 24 hour time, so subtracting 12 // puts it in 12 hour time hours -= 12; sPrefix = "P.M."; } else { sPrefix = "A.M."; } if (minutes < 10) { sMinutes = "0" + String(minutes); } else { sMinutes = String(minutes); } if (seconds < 10) { sSeconds = "0" + String(seconds); } else { sSeconds = String(seconds); } lblTime.text =String(hours) + ":" + sMinutes + ":" + sSeconds + " " + sPrefix; }
You can view the result here.Code:clockTimer.start();
In the next tutorial, we will look at how to stop timers. We will also look at how to create timers that don't run forever.
What, no screenshots this time?
Nicely done. +rep
There wasn't a need for screen shots this time.![]()
Superb!
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks