Groups | Blog | Home
all groups > flash actionscript > june 2006 >

flash actionscript : clearInterval


alrazzle
6/3/2006 11:58:51 PM
I have a website that has several pages on it that are accessible through a nav
bar at the top of the site. On the home page there is a continuous playing
slideshow in a movieclip. when I go to another page and then back to the
homepage the slideshow doesn't work sometimes, or other times it will play one
of the images multiple times, and other times will skip images.

I have the slideshow running on an interval, and have tried using
clearInterval to reset the slideshow when clicking off of the main page, so
that upon returning to it, it will start again. I must be missing something to
either clear the interval when going to another page or make the slideshow
start over again when going back to the home page.

Any ideas?

thanks!:confused;
kglad
6/4/2006 3:27:55 AM
AkiraX
6/5/2006 4:12:33 PM
One way that can solve your problem is saving your intervalID in an array. and
when you do want to clear de interval, you clear the all array with de
intervalID's.

That can solve problems when you create an intervalID,and then create another
intervalID without clearing the first one. (This happens when an aplication is
processing and its creates an intervalID before deleting the previous one, for
example)

Maybe it can solve your problem! :P
kglad
6/6/2006 2:21:38 PM
there's no need to store all the intervals in an array and then clear them all
prior to the declaration of all new setInterval() statements. simply execute
clearInterval() just prior to all setInterval() statements, using the interval
id of the about to be created interval.
alrazzle
9/30/2006 6:21:29 PM
Sorry I took so long to come back to this. School started up and I gave up on
the project for a while.

Below is my code. I must be doing the clearInterval() incorrectly

var nInterval:Number;
var fadeIn:Boolean = true;

this.image1._alpha = 0;

fadeIn = true;
play();
nInterval = setInterval(fadeImage, 50, this.image1);

function fadeImage(mcImage:MovieClip):Void {
if(fadeIn) {
mcImage._alpha += 4;
if (mcImage._alpha >= 100) {
fadeIn = !fadeIn;
}
} else {
mcImage._alpha -= 4;
if (mcImage._alpha <= 0) {
fadeIn = !fadeIn;
clearInterval(nInterval);
}
}
updateAfterEvent();
};
kglad
9/30/2006 9:46:37 PM
your code looks ok as long as it's not attached to a frame that executes more
than once while a loop is initiated. to prevent that problem use clearInterval
just prior to setInterval():



var nInterval:Number;
var fadeIn:Boolean = true;

this.image1._alpha = 0;

fadeIn = true;
play();
clearInterval(nInterval);
nInterval = setInterval(fadeImage, 50, this.image1);

function fadeImage(mcImage:MovieClip):Void {
if(fadeIn) {
mcImage._alpha += 4;
if (mcImage._alpha >= 100) {
fadeIn = !fadeIn;
}
} else {
mcImage._alpha -= 4;
if (mcImage._alpha <= 0) {
fadeIn = !fadeIn;
clearInterval(nInterval);
}
}
updateAfterEvent();
};
kglad
9/30/2006 9:49:05 PM
and if you're calling fadeImage using different setInterval() functions and
passing different images you should use:



var nInterval:Number;
var fadeIn:Boolean = true;

this.image1._alpha = 0;

fadeIn = true;
play();
clearInterval(this.image1.nInterval);
this.image1.nInterval = setInterval(fadeImage, 50, this.image1);

function fadeImage(mcImage:MovieClip):Void {
if(fadeIn) {
mcImage._alpha += 4;
if (mcImage._alpha >= 100) {
fadeIn = !fadeIn;
}
} else {
mcImage._alpha -= 4;
if (mcImage._alpha <= 0) {
fadeIn = !fadeIn;
clearInterval(mcImage.nInterval);
}
}
updateAfterEvent();
};
AddThis Social Bookmark Button