Cant use setInterval function properly in actionscript....

Hi,
This is one function in my action script. I am calling this function on my buttons "release" event.
================================
on (release) {
go1();

var intervalId:Number;
function go1(){
trace("Hello!!!");
intervalId = setInterval(go2(),3000)
clearInterval(intervalId);
}

function go2(){
intervalId = setInterval(go1(),3000);
clearInterval(intervalId);
}
}
================================

I guess In above function when user clicks on button it should trace mesage "Hello!!!" in the interval of 6000 miliseconds. But its not happening here.
When I am clicking on button at that time only once I am getting followin output with error message:-
================================
Hello!!!
Hello!!!
Hello!!!
Hello!!!
256 levels of recursion were exceeded in one action list.
This is probably an infinite loop.
Further execution of actions has been disabled in this movie.
================================

Why I am getting this error? & What should i do to get the above output message in interval of 6000 miliseconds.
Please help me.
Thanx in advance.
Prash
EconifyAsked:
Who is Participating?
 
wings_gauravConnect With a Mentor Commented:
on (release)
{
var intervalId:Number;
function go1()
{
      trace("Hello!!!");
      clearInterval(intervalId);
}

intervalId = setInterval(go1,6000)
}

should work fine.

The problem is with
intervalId = setInterval(go1(),3000); // go1() should be just go1 without brackets
when you say this - you are calling go1 not passing function name as reference.



- wings
0
 
Aneesh ChopraCommented:
Hi gaurav,

your code will only show the "Hello!!!" once and that even after 6000 milliseconds of clicking..


here I have fine-tune the code, it will show the "Hello!!!" as user clicks and then after every 6000 milliseconds.
----------------
on (release) {
      var intervalId:Number;
      if (intervalId != undefined) {
            clearInterval(intervalId);
      }
      function go1() {
            trace("Hello!!!");
      }
      go1();
      intervalId = setInterval(go1, 6000);
}
---------------

Rgds
Aneesh
0
 
wings_gauravCommented:
Mr. Aneesh,

It would be advisable if you read the requirnments posted by the user before providing your expert comments.

Best Regards,
Gaurav
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.