0

After a function has run I want to disable a whole set of functions can be executed again. I thought the most logical way would be to put them all together inside an overarching function (in this case function run) and disable that function for some time.

So for example in the code below, when function exampleOne is executed I want to disable the whole run function for X time before it can run again.

function run(){
  function exampleOne(){
    // Code
    sleep(1000);
  }

  function exampleTwo(){
    // Code
    sleep(1000);
  }
}
run();
1
  • 1
    You're looking for throttling. Commented Aug 9, 2018 at 16:33

2 Answers 2

2

Take a look at underscore.js and especially the throttle or debounce functions.

Sign up to request clarification or add additional context in comments.

Comments

1

You can define your functions as variables then you can disable them by assigning an empty or error functions to these variables. Two functions (e.g. enable/disable) can be responsible to disable/enable the whole set of functions. and of course you can use setTimeout to manage the delay.

for example:

var exampleOne, exampleTwo;

function enable(){
    exampleOne = function(){       
        // do something

        disable();
        setTimeout(function() { enable(); }, 10000);
    }

    exampleTwo = function(){
        // do something

        disable();
        setTimeout(function() { enable(); }, 10000);
    }     
}

function disable(){
    exampleOne = function(){
        Alert("i'm sleep!")
    }

    exampleTwo = function(){
        // do nothing
    }     
} 

enable();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.