Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

call a funtion at regular interval

New Here ,
Jun 01, 2009 Jun 01, 2009

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.

TOPICS
ActionScript
889
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 01, 2009 Jun 01, 2009

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 01, 2009 Jun 01, 2009

Actually what I need is, setInterval function should not start defaulty. whenever I want, I will have to start as well as clear.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 01, 2009 Jun 01, 2009

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 01, 2009 Jun 01, 2009

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 01, 2009 Jun 01, 2009

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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 01, 2009 Jun 01, 2009

Thank you for your kindness...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 01, 2009 Jun 01, 2009
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines