Let us assume that we have been given a function add() which takes in two parameters a and b, and returns their sum. We are supposed to implement a function that should be able to call any function after the given delay amount of time. This can be done with the approaches as given below:
Approach 1: Create a function that takes in the amount of delay, the function to be called, and the parameters to be given.
Example: In this approach, we will pass the add() function along with its parameters to the callAfter() we have created. The setTimeout() method is used to call the add() function from inside the callAfter() method after the given delay amount of time. The parameters are also passed to the function.
Approach 2: Using an anonymous function.
Example: In some cases, you may want to pass a function with arguments, but not want it to be called until the callback is invoked. In this case, you could wrap it in an anonymous function.
functionName.callAfter(delay, param1, param2, ....);
Example: This example shows the above-explained approach.
Output: An alert message is displayed after the specified delay for all the approached mentioned above.