- Home
- Tutorials
- Flash
- Intermediate
- Intervals in Flash with setInterval

More applications of setInterval
Jesse Stratford
Jesse lives in Melbourne Australia and is the Cofounder and webmaster of http://ActionScript.org. Once a full-time Flash enthusiast, teacher, author, freelancer and speaker Jesse's now leads a double life -- management consultant by day, http://ActionScript.org director by night. He enjoys participating actively in community and the wider Flash scene when he has time.
View all articles by Jesse Stratfordfunction 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. |
Spread The Word
Related Articles
4 Responses to "Intervals in Flash with setInterval" 
|
said this on 19 May 2007 10:33:36 AM CST
Just wanted to point out
Cheers. |
|
said this on 25 Mar 2009 12:15:14 PM CST
Thanks. I've made the upd
|
|
said this on 13 Jul 2007 2:35:15 AM CST
if you use updateAfterEve
Just a thought |
|
said this on 25 Mar 2009 8:40:34 AM CST
Thanks smith for pointing
Otherwis |



Author/Admin)