How to disable a function for X seconds when another button clicked in As3?

184 views Asked by At

I have simple question that it became complected for me which is:

I have a function which is called and run after around 11 seconds:

var myTimeoutClickObject = setTimeout(clickObject,11000); 
function clickObject():void{
  //My code
}

I also have a Reset Button that I made a Click Function for it:

reset_btn.addEventListener(MouseEvent.CLICK, reset);
function reset(e:MouseEvent):void{
  //My code
}

Now, I want to make the clickObject() function disabled for around 11 seconds again every time that I click on the reset_btn Button.

I really appreciate, if you can help me in this matter!

Thanks in advance!

1

There are 1 answers

0
Nemi On

You can make it using flag:

var isClickDisabled:Boolean = false;

var myTimeoutClickObject = setTimeout(clickObject, 11000);

function clickObject():void{
    //My code
    if(isClickDisabled)
       return;
}

reset_btn.addEventListener(MouseEvent.CLICK, reset);

function reset(e:MouseEvent):void{
  //My code
  isClickDisabled = true;

  // call code to re-enable it later for 11sec
}

Or use clearTimeout like this:

clearTimeout(myTimeoutClickObject);

setTimeout(startAgain_clickObject, 11000);

function startAgain_clickObject(e:MouseEvent):void {
      myTimeoutClickObject = setTimeout(clickObject, 11000);
}