Simple Flash Timer
by leon on March 11, 2008
I’ve not written an ActionScript related post for a while, so thought I would share a way to make a simple timer function in Flash. This has all sorts of uses. I currently use it to create a tooltip when a user rolls over a button. After a few seconds delay the tooltip appears. Other uses included looping through an array. I may not want this to happen instantaneously but after a short delay. Implementing this script can achieve this and more.
The script takes advantage of the setInterval() function in flash. This calls a function or a method of an object at periodic intervals while a SWF file plays. You can use setInterval() to execute any function repetitively over time.
Parameters
functionReference:Function – A reference to the function to be called.
interval:Number – The time in milliseconds between calls to the functionReference or methodName function passed in.
If interval is less than the SWF file’s frame rate (for example, 10 frames per second [fps] is equal to 100 millisecond intervals), the interval function is called as close in time to the value of interval as possible. Executing long, memory-intensive scripts during an interval causes delays. If the function being called initiates a change to visual elements, you should use the updateAfterEvent() function to make sure that the screen refreshes often enough. If interval is greater than the SWF file’s frame rate, the interval function is called only after interval has expired and the playhead has entered the next frame; this minimizes the impact each time the screen is refreshed.
param:Object [optional] – Parameters passed to the function that was sent to functionReference or methodName. Multiple parameters should be separated by commas: param1,param2, ...,paramN
objectReference:Object – An object that contains the method specified by methodName.
methodName:String – A method that exists in the scope of the object specified by objectReference.
Returns
Number – An integer that identifies the interval (the interval ID), which you can pass to clearInterval() to cancel the interval.
The code is as follows:
//Define the function var myTimer;
//Define a counter var count:Number = 0;
//Define a max value for the counter var maxCount:Number = 5;
//Define a count duration in millseconds 1000 = 1 second var duration:Number = 1000;
//call the function after a button is clicked
startBtn.onPress = function() {
myTimer = setInterval(startTimer, duration);
//disable the button the stop multiple function calls
startBtn.enabled = false;
};
function startTimer() {
//display the count. this can be omitted and is just for show
myOutput.text = "Counting " + (count + 1);
//if statement. after the counter runs for the maxCount (in this case 5 seconds, do something
if (count >= maxCount) {
//clear the interval. thos stops memory leaks
clearInterval(myTimer);
count = 0;
myOutput.text = "Call function";
startBtn.enabled = true;
}
//increment the counter
count++;
}
Downloads: FLA

Leave your comment