What it does (line by line):
  1. Perform these actions every time the frame changes (even if your _root level is stopped these actions will still continue to be performed 12 times a second, assuming your fps is 12).
  2. If the _root level variable delay is true;
  3. If the _root level variable waitHowLong is less than or equal to the current time in seconds minus the time at which we began counting
  4. Comment - Insert your actions on this line
  5. Set the _root level variable delay to False.
  6. Else for like 3's If
  7. Comment just for neat coding purposes
  8. Close Line 6's Else
  9. Close Line 2's If
  10. Close Line 1's Clip Event.

So you can see that, given a few variables, this code will loop until the time is right, then execute the actions on Line 4. Those variables are:

_root.delay - Boolean variable, marks if we are running a delay loop at the moment. This allows us to save on CPU usage if we are not currently running a delay, because if delay = false the rest of the actions in this code are skipped

_root.waitHowLong - A number variable (Integer or Floating point) which states the time in seconds we wish to wait before carrying out Line 4's actions

_root.startTime - Floating point variable set upon initiation of delay, see below.

Triggering:

Having now setup the engine which will control our delay we need to give it some info. This trigger info can be sent from a keyframe or a button. Let's look at a button:

on (release) {
        _root.delay = true;
        _root.startTime = (getTimer()/1000);
}

This code, placed upon a button will set the delay variable to true and thus initiate our delay loop. It also sets the startTime variable (to the current number of seconds since the movie began). So in order for this to work you also need to put this code somewhere:

_root.waitHowLong = 5;

(This makes a 5 second delay).

The best thing about this script is that it allows you to change the length of the delay dynamically, as shown here:

on (release) {
        _root.waitHowLong = random(10)+1;
        _root.delay = true;
        _root.startTime = (getTimer()/1000);
}

This button will create a delay of a random number of seconds.

Known Limitations:

Like they say in Seinfeld, "1 Chip, 1 Dip!". If you want to run more than one concurrent delay you will need more than one controller movie clip and you will also need to create a whole new set of variables (delay2, startTime2, etc)

That's it really!

Jesse Stratford is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash.

NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there.

If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity.
This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it.