Also, using the same principle we can account for the bandwidth problem noted in the example above: a user can select how often they want to get updates, perhaps by specifying how many times per minute. Using a simple math manipulation we can convert this to an argument for setinterval.

function updateStockPrices(whichStock) {
        // Update code here
        trace('Updating prices for '+whichStock);
}
updatesPerMinute = 6;
stockToCheck = "Stratford Flash Products";
stockInterval = setInterval(updateStockPrices, (60/updatesPerMinute) * 1000, stockToCheck);

Disposable Culture:

Often enough we want to perform an operation just once after X seconds, then never again. In Flash 5 we needed to use a bit of a convoluted method which was very inefficient, but in Flash MX we can just use setinterval as follows:

function mytrace(str) {
        trace(str);
        clearInterval(doOnce);
}
doOnce = setInterval(mytrace, 1000, "This is a test!");

Easy peasy.

Efficiency:

My next tutorial will likely be about listeners and how to use them. setinterval is good for rapid response systems but it shouldn't be used for checking input in a game environment or anything like that in my opinion. The new event model in Flash MX provides better ways to do such things. See the tutorial by FlashGuru in the Intermediate section of this site.

Also it's important to note that, while the interval is specified in thousandths of seconds, you cannot accurately perform a setinterval loop on too small an interval. For example, try out this code:

function mytrace(str) {
        trace(getTimer());
}
tooShortInt = setInterval(mytrace, 10);

The first thing to note is that the code takes a moment to become active, as shown by the first few entries, which are obviously not 10 thousandths of a second apart:

59, 119, 135, 146, 189, 206

Even after a few seconds, the results are still not coming at the correct interval:

6046, 6062, 6078, 6104

On my XP1800 the results don't become reliable until the interval is up around 170 thousandths of a second (regardless of the movie's frame rate).

As always, feedback, suggestions, thanks and constructive criticism are welcome via email. Cheers.

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.