<!-- This example is from the book _JavaScript: The Definitive Guide_.     -->
<!-- Written by David Flanagan.  Copyright (c) 1996 O'Reilly & Associates. -->
<!-- This example is provided WITHOUT WARRANTY either expressed or implied.-->
<!-- You may study, use, modify, and distribute it for any purpose.        -->

<!-- The image that will be animated.  Give it a name for convenience -->
<IMG SRC="images/0.gif" NAME=animation>

<SCRIPT>
var frame = 0;         // keep track of what frame of the animation we're on.
var timeout_id = null; // allows us to stop the animation.

function animate()  // The function that does the animation.
{
    document.animation.src = images[frame].src;
    frame = (frame + 1)%10;
    timeout_id = setTimeout("animate()", 250);  // display next frame later
}

// Count how many images have been loaded.  When we reach 10, start animating
function count_images() {  if (++num_loaded_images == 10) animate(); }
var num_loaded_images = 0;

// Create the off-screen images and assign the image URLs.
// Also assign an event handler so we can count how many images have been
// loaded.  Note that we assign the handler before the URL, because otherwise
// the image might finish loading (if it is already cached, e.g.) before
// we assign the handler, and then we'll lose count of how many have loaded!
images = new Array(10);
for(var i = 0; i < 10; i++) {
    images[i] = new Image();                 // Create an Image object
    images[i].onload = count_images;         // assign the event handler
    images[i].src = "images/" + i + ".gif";  // tell it what URL to load
}
</SCRIPT>

<!-- Buttons to control the animation.  Note that we don't let the user
  -- start the animation before all the images are loaded -->
<FORM>                   
  <INPUT TYPE=button VALUE="Start" 
         onClick="if (!timeout_id && num_loaded_images==10) animate()">
  <INPUT TYPE=button VALUE="Stop" 
         onClick="if (timeout_id) clearTimeout(timeout_id); timeout_id=null;">
</FORM>
