PDA

View Full Version : Can't get setInterval to work


Langy
03-31-2006, 12:21 AM
Ok I'm really having troubles with setInterval.

I have created a loop to load in 8 mc's and at the same time I want a short delay between each one.

No matter what I try I still can not get this to work. I've tried placing the clearInterval where it is shown and also in the next line after the setInterval.

At the moment, I think there is a short delay before the mc's are loaded but then it loads them all at once.

Any assistance much appreciated.

for (lp=1; lp<9; lp++) {
interval = setInterval(LoadGallery, 1000,lp);
}

function LoadGallery(i) {
var mcIdent = "Gal" + i + "_mc";
_root.createEmptyMovieClip("Gal" + i + "_mc", 50+i);
_root["Gal"+i+"_mc"].attachMovie("Pic0" + i,"Pic0" + i,250+i);
_root["Gal"+i+"_mc"]._xscale = 25;
_root["Gal"+i+"_mc"]._yscale = 25;
_root["Gal"+i+"_mc"]._x = galx[i-1];
_root["Gal"+i+"_mc"]._y = galy[i-1];
clearInterval(interval);
}

mrand01
03-31-2006, 08:12 PM
The setIntervals are getting called at nearly the same time because they are in a for loop. To do something like this, here's how it should look:


curPic = 0;

function LoadGallery(max)
{
//your code here, just use curPic instead of i;
if (curPic == max)
{
clearInterval(interval);
}
else
{
curPic++;
}
}

interval = setInterval(LoadGallery, 1000, max);


where i is the current number picture to load, and max is the total ammt of pictures you are loading.

Langy
03-31-2006, 09:27 PM
Many thanks.

Now I can see how the setInterval works.
It will run whats inside all the time until it is cleared, so it acts like its own loop but at a set speed.

Excellent, thanks again.