ActionScript: Repeating a Task at Timed Intervals

by Gabriel Savimbi.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on flash  

You are here: Categories » Computers and technology » Flash

You want to perform an action or actions at a specific timed interval.

Use the setInterval( ) function.

The setInterval( ) function allows you to specify an interval (in milliseconds) at which your Flash movie will invoke a function. Use setInterval( ) to perform a particular action over time but not necessarily at the frequency of the frame rate of the movie.

// Define a function.
   function myIntervalFunction (  ) {
 // Output the difference between the current timer value and its value from the
   // last time the function was called.
   trace(getTimer(  ) - lastTime);
   lastTime = getTimer(  );
   }
// Set up an interval that attempts to invoke myIntervalFunction(  ) once every
   // millisecond.
   setInterval(myIntervalFunction, 1);

In the preceding example, even though the interval is theoretically one millisecond, in practice, its accuracy and granularity depend on computer playback performance in relation to other tasks being demanded of the processor. There are two implications to this:

Don't rely on intervals to be extremely precise.

Don't rely on intervals to be smaller than a few milliseconds.

The setInterval( ) function returns an identifier for the newly created interval. If you want to be able to stop the interval at a later time, you must store the return value, as follows:

// Set an interval such that someFunction(  ) is called approximately once per second.
   // Assign setInterval(  )'s return value to the variable myIntervalID for later use.
   myIntervalID = setInterval(someFunction, 1000);

You can use the clearInterval( ) function to stop an interval if you know the interval's identifier:

clearInterval(myIntervalID);

If you want the interval to invoke the method of an object instead of a standalone function, you can use the variation of the setInterval( ) function in which you pass it three parameters—a reference to the object, the name of the function (as a string), and the interval in milliseconds— instead of just two:

// Create a simple object using the Object constructor.
   obj = new Object(  );
// Assign a method named myMethod to an object, obj.
   obj.myMethod = function (  ) {
   trace("obj.myMethod(  ) has been called");
   };
// Use setInterval(  ) to tell the movie to invoke the myMethod(  ) method of the obj
   // object approximately every six seconds.
   setInterval(obj, "myMethod", 6000);

Whichever variation of the setInterval( ) function you use, any additional parameters that you pass to the setInterval( ) function are passed along to the function or method:

// Define a function that accepts a parameter and displays it in the Output window.
   function displayValue (val) {
   trace(val);
   }
// Use setInterval(  ) to call displayValue(  ) once per minute. The third parameter is 
   // passed to the function when it is called so that each time "Bunny rabbits go 
   // hippity-hop" is displayed in the Output window.
   setInterval(displayValue, 60000, "Bunny rabbits go hippity-hop");

Be aware that any values that you pass to a function or method by way of the setInterval( ) function are evaluated only at the time the interval is initialized. So the same parameter values are always passed to a function or method that is called via setInterval( ):

obj = new Object(  );
obj.traceAnimalName = function (name) {
   trace(name);
   };
myAnimalName = "cub";
setInterval(obj, "traceAnimalName", 30, myAnimalName);
// Even if myAnimalName is assigned a new value, the value "cub" is always passed to
   // traceAnimalName(  ), because myAnimalName was "cub" when setInterval(  ) was first
   // called.
   myAnimalName = "puppy";

One of the neat things you can do with setInterval( ) is create animations that are independent of the movie's frame rate. Remember that the onEnterFrame( ) method executes at the same interval as the frame rate, so using that technique ties you to the movie's properties. But with setInterval( ) you can call a function or method at any interval you want. Here is an example in which two intervals are set—one for a square movie clip (every 50 milliseconds) and one for a circle movie clip (every 100 milliseconds):

// Define the function first. This function takes three parameters: a reference to 
   // the movie clip object, the change in x, and the change in y.
   function moveObj (obj, dx, dy) {
 // Increment the movie clip's x and y coordinates.
   obj._x += dx;
   obj._y += dy;
 // In case the interval is less than the movie's frame rate, you need to use the 
   // built-in updateAfterEvent(  ) method to refresh the Stage.
   updateAfterEvent(  );
   }
// Create two intervals. Each invokes the moveObj(  ) function, but at different 
   // intervals and with different movie clip references as parameters.
   squareInterval = setInterval(moveObj, 50,  square, 1, 1);
   circleInterval = setInterval(moveObj, 100, circle, 1, 1);
Leave a comment or ask a question
Total comments: 0

Flash Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
Avoiding Conflicting Variables in ActionScript - You want to make sure that variables within a function do not interfere with variables in other functions or within the timeline in which the function is defined. Use the var keyword to declare loc (more...)
How to Exit a Function in ActionScript - You want to exit a function. Functions terminate automatically after the last statement within the function executes. Use a return statement to exit a function before reaching its end. The (more...)
Move Clip Rotation - A property like _x and _y is the movie clip property _rotation. The _rotation property accepts a value in degrees. A circle is divided into 360 degrees. The values used by _rotation range f (more...)
Targeting Flash Movie Clips - The simplest way to target a movie clip is to use its name, followed by a dot, followed by the command you want to send. However, there are plenty of other ways to target a movie clip as we (more...)
Using Mathematical Operators in ActionScript - You want to modify something over time, such as the rotation or position of a movie clip. Use the compound assignment operators to change a variable or property in increments. Or, if increm (more...)
How to build an animated button using Flash - Time: 15 Minutes. Difficulty Level: Intermediate Requirements: Flash 8 Assumed Knowledge: Basic action (more...)
Enhancing Standalone Projector - You want to create an enhanced Standalone Projector with features such as borderless playback, custom titles, no Flash menus, and so on. Use a third-party tool such as SWF Studio or SWFKit to c (more...)
Creating Flash Buttons - Buttons are one of the three main types of symbols that you use in Flash. The others are movie clips and graphics. Making a New Button There are many ways to create a button. One (more...)
Bouncing Ball Script - Start a new Flash movie. Create a movie clip that has a ball graphic inside it. You can name the instance of the movie clip, myClip, but our code will not depend on the name of the clip. (more...)
ActionScript: Creating Reusable Code - You want to perform a series of actions at various times without duplicating code unnecessarily throughout your movie. Create a function and then call (i.e., invoke) it by name whenever you (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.