Groups | Blog | Home
all groups > flash (macromedia) > june 2005 >

flash (macromedia) : clearinterval



dan mode
6/28/2005 5:48:16 PM
you need to add a break if it gets to the amount you want
[quoted text, click to view]

yaniv73
6/28/2005 11:34:18 PM
hi

i am trying to loop an _alpha for a movie clip so i used this script :

alpha();
function alpha()
{
i=i+1;
title_mc._alpha = i;
if (i<101)
{
trace(i);
var intervalID:Number =setInterval(alpha,200);
}
else
{
clearInterval(intervalID);
trace(intervalID);
}
}

everything is working except the clearinterval so i get an OVERFLOW

why isn't the clearinterval working? what am i doing wrong?

thanks
yaniv73
6/29/2005 12:13:02 AM
jecky thanks

yes i do initialise the 'i' before the alpha();
i have var i=0;
and as i said it is increasing by 1 everytime but the prolem as you said is
with the clearinterval and i know that i am doing something wrong but what is
it?

if you can explain it to me it will be helpfull
thanks again
NSurveyor
6/29/2005 1:34:20 AM
Your code is confusing. When you call alpha for the first time i is 1. Since i
is less than one, alpha will be called at an interval of 200 milliseconds.
After 200 milliseconds, the function is called again, HOWEVER another
setInterval will be called. Eventually you will have MANY intervals running but
one variable that is the interval id. Why not do something like this?

alpha(title_mc);
function alpha(mc){
mc._alpha = 0;
alphaOnce = function(mc){
mc._alpha++;
if(mc._alpha>=100){
clearInterval(mc.intervalID);
}
}
mc.intervalID = setInterval(alphaOnce,200,mc);
}
NSurveyor
6/29/2005 1:42:56 AM
yaniv73
6/29/2005 3:02:15 AM
thanks jecky youu are great!!

Jeckyl
6/29/2005 9:54:43 AM
That code is very confused re intervals. Its putting interval number in a
local variable that disappears by the time you try to clear it .. so it will
never get cleared. You don't initialise variable 'i'. so it won't increment
properly
--
Jeckyl

Jeckyl
6/29/2005 11:24:18 AM
why do you have multiple intervals though ... why not have one interval that
runs at the given interval and until the alpha reaches a given level and
then stops. it would be much simpler than having intervals the spawn other
intervals. eg. This code does it nicely and cleanly, without creating any
extra timeline variable etc .. all self-contained in the function.

function alphaFadeIn() {
// keep track of alpha and interval number in 'settings' object
var settings = new Object;;
settings.alpha = 0;
// set off the fade interval, remembering the interval number in
settings
settings.interval = setInterval (
function (settings) {
// bump up the alpha
settings.alpha += 1;
trace(settings.alpha);
title_mc._alpha = settings.alpha;
// if faded in to 100%, then clear interval so we stop
if (settings.alpha >= 100) {
clearInterval(settings.interval);
}
}
,200,settings
);
}
alphaFadeIn();

--
Jeckyl

Jeckyl
6/29/2005 11:47:46 AM
[quoted text, click to view]

you don't need one.
--
Jeckyl



Jeckyl
6/29/2005 12:00:41 PM
NSurveyor .. I'm afraid you code has an (actual) bug. alphaOnce is a
timeline variable of the clip where the code live, but then you refer to it
in the clearInterval as mc.alphaOnce .. and the mc is not the same clip.

In any case, this method of keeping track of the interval id is not good.
eg if you call an interval more than once, then you'll lose the old interval
id, as there is just one variable being used to keep track of it. As a
technique, it is better for the interval to keep track of its own interval
id so it can kill itself, and not rely on the caller (or other clip) to keep
the variable holding the interval id. BTW: Here is a slight variation on my
code from before that passes the clip and the interval delay to the
function:

function alphaFadeIn(clip, intervalDelay) {
// keep track of alpha, clip and interval number in 'settings' object
var settings = new Object;
settings.alpha = 0;
settings.clip = clip;
// set off the fade interval, remember interval number in settings
settings.interval = setInterval (
function (settings) {
// bump up the alpha
settings.alpha += 1;
settings.clip._alpha = settings.alpha;
// if faded in to 100%, then clear interval so we stop
if (settings.alpha >= 100) {
clearInterval(settings.interval);
}
},
intervalDelay, settings
);
}
alphaFadeIn ( title_mc, 200 );
--
Jeckyl

[quoted text, click to view]

AddThis Social Bookmark Button