Copy link to clipboard
Copied
hi all, how a function can be called at regular interval for limited times in AS3.0 and one more thing the timer should not be started automatically. I have to trigger that method.
Copy link to clipboard
Copied
you already know about the timer class? if not, that's what you should use. check the flash help files.
if yes, what problem are you having?
Copy link to clipboard
Copied
Actually what I need is, setInterval function should not start defaulty. whenever I want, I will have to start as well as clear.
Copy link to clipboard
Copied
i'm not sure what you're trying to say.
setInterval() starts calling its function when the interval is instantiated and stops when the interval is cleared.
you can define a timer, add a listener and define a listener function and start it whenever you want and stop it whenever you want.
Copy link to clipboard
Copied
var count:Number = 0;
function generateRandom():void {
count++;
if (count >= 6) {
clearInterval(IntervalID);
}
}
function toCallFn(e:Event):void {
var IntervalID:int = setInterval(generateRandom, 1000);
}
start_Btn.addEventListener(MouseEvent.CLICK,toCallFn);
if I compile the above script, I will get the below error.
1120: Access of undefined property IntervalID.clearInterval(IntervalID);
How can I rectify this.
Copy link to clipboard
Copied
when you prefix a variable with var inside a function, you're making that variable local to that function (and just that function call in which it's instantiated). you don't want IntervalID to be local to toCallFn(). use:
var IntervalID:int;
var count:Number = 0;
function generateRandom():void {
count++;
if (count >= 6) {
clearInterval(IntervalID);
}
}
function toCallFn(e:Event):void {
IntervalID = setInterval(generateRandom, 1000);
}
start_Btn.addEventListener(MouseEvent.CLICK,toCallFn);
Copy link to clipboard
Copied
Thank you for your kindness...
Copy link to clipboard
Copied
you're welcome.
p.s. you should add a clearInterva() to the line just above your setInterval(). if you click your button a 2nd time before the first interval has cleared, you'll have a mess.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now