Flash ActionScript Timer Class Clock with Start and Stop Buttons
This Flash ActionScript Timer Class tutorial demonstrate how to use a Play button to start the timer clock and a Stop button to stop the timer clock.
Flash Tutorial Content:
In this Flash Timer Class tutorial, we provide a Play button to start the timer clock and a Stop button to stop the timer clock.
The complete Flash Movie of this tutorial is shown as above. Click on the Flash movie to activate it and play around to see how it works. The timer clock will start counting when the Play button is clicked and stop when the Stop button is clicked.
Flash ActionScript Codes:
//Create a Timer object and store it in a variable called myClock.
//The timer will trigger every 100 milliseconds (0.1 seconds)
//1000 milliseconds = 1 second
//The repeatCount is zero, i.e. timer will trigger infinitely
//Summary:
//Create a Timer instance that will trigger every 0.1 seconds infinitely
var myClock:Timer = new Timer(100, 0);
//Start the time at zero
var time:Number =0;
function countingClock(evt:TimerEvent):void {
time += .1;
//Only need one decimal place of myClock
time = (Math.round(time*10)) / 10;
//Show the time
clock_txt.text = String(time);
}
//Add an event listener to the timer object (myClock)
//The event that listen is called TIMER which will be trigger every 0.1 seconds
//Every time the TIMER event is triggered, it will call the countingClock function
myClock.addEventListener(TimerEvent.TIMER, countingClock);
function startClock(evt:MouseEvent):void {
//Start myClock when startClock function is called
myClock.start();
}
play_btn.addEventListener(MouseEvent.CLICK, startClock);
function stopClock(evt:MouseEvent):void {
myClock.stop();
}
stop_btn.addEventListener(MouseEvent.CLICK, stopClock);
Download Flash Source File:
Remarks:
Up to now, the delay of the Timers are set before we used them. Actually the delay of Timer can be adjusted (faster or slower) during the runtime. In the next Flash Timer Class animation tutorial, we will use two buttons to control the delay of a Timer.