Table of Contents

8.8 An Alternative to Timeline Loops:setInterval( )

As of Flash Player 6, we can use setInterval( ) to execute a function every n milliseconds. For example, the following code creates a function, moveBall( ), that moves the ball_mc movie clip 10 pixels to the right, and then uses setInterval( ) to execute that function every 20 milliseconds.

moveBall = function ( ) {
  _root.ball_mc._x += 10;
}
ballMoverID = setInterval(moveBall, 20);

To stop our interval, we can later use:

clearInterval(ballMoverID);

Like a timeline loop or an onEnterFrame( ) loop, setInterval( ) can execute a block of code indefinitely, and it allows for screen updates between iterations. However, the setInterval( ) function is not tied directly to a movie's frame rate (though it is affected by it). For complete details, see setInterval( ) in the ActionScript Language Reference.


Table of Contents