Category: Flash

setInterval pains

Are you coding away in flash using setInterval instead of onEnterFrame in order to be a bit nicer on the CPU that has to render your final flash piece? Bashing your head against the wall because they aren’t clearing properly, or failing to fire properly? Well here’s my quick attempt at trying to explain how to use setInterval.

To start with, always start with an interval ID. This is just a variable that you create to hold a number that references the current setInterval occurrance (wanted a better word, but that will do). By doing this it is much easier to turn it off when you no longer need it.

Once we have that variable, we go ahead and clear the interval. I know it seem counter-intuitive, but over time I’ve found that by clearing it first, you are guaranteed not to end up with multiple instances of the same setInterval messing everything up on you. So here we go:

var newInt:Number;
clearInterval(newInt);
newInt = setInterval(ding, 1000, “ding”);

So all we have here is a setInterval command, ready to fire off a function called ding every second (remember that setInterval works in milliseconds and there are 1000 milliseconds to a second) sending the parameter of “ding”. So lets add in the function:

var newInt:Number;
clearInterval(newInt);
newInt = setInterval(ding, 1000, “ding”);

function ding(txt:String):void{
    trace(txt);
}

This is now in a position where you can go ahead and try it out so press CTRL-Enter and preview it. If all goes to plan your output will trace “ding” once every second.

To test things out a bit more, let’s introduce a second setInterval command to complicate it all and fire off the original setInterval at random times. The code will look like this:

var newInt:Number;
var secondInt:Number;
var count:Number = 0;

function init(){
   secondInt = setInterval(startInterval, 1000);
}

function startInterval(){
   count++;
   clearInterval(newInt);
   newInt = setInterval(ding, 500, “ding – ” + count);
}

function ding(txt:String){
    trace(txt);
}

init();

So what this is going to do is fire off startInterval every second which will then fire our ding function half a second after that. Because the clearInterval(newInt) is called before the next setInterval is set, everything happens once, then ends. I added in the count to make this easier to notice.

Experiment with this until you have it all figured out. Try commenting out the clearInterval(newInt); line and see what happens:

var newInt:Number;
var secondInt:Number;
var count:Number = 0;

function init(){
   secondInt = setInterval(startInterval, 1000);
}

function startInterval(){
   count++;
   //clearInterval(newInt);                                 
   newInt = setInterval(ding, 500, “ding – ” + count);
}

function ding(txt:String){
    trace(txt);
}

init();

You’ll see now that each iteration of the newInt setInterval keeps going, repeating itself every half a second with nothing to stop it. As far as I can tell there’s also no way of rescuing setIntervals that are launched without a suitable ID.

So the basic rules of setInterval are:

  • Always use clearInterval before a setInterval;
  • Always use an interval ID;
  • Use a unique interval ID for each setInterval you need.

Using Flashvars with Actionscript 3 projects

Recently I decided to try and make a generic video player that any old monkey could use to play FLV’s and other Flash compatible videos.

The obvious way (to me) to create it so that you would never need to edit the swf was to pass the path to the source video via flashvars.

Continue reading »