How to implement a function that enable another function after specified time using JavaScript ? Last Updated : 30 Dec, 2022 Comments Improve Suggest changes Like Article Like Report 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. JavaScript <script> function callAfter(delay, myFunct, ...params) { setTimeout(() => { myFunct(...params); }, delay); } function add(a, b) { alert("The sum is : " + (a + b)); } callAfter(2000, add, 4, 7); </script> Output: 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. JavaScript <script> function callAfter(delay, funct) { setTimeout(() => { funct(); }, delay); } function add(a, b) { alert("The sum is : " + (a + b)); } callAfter(2000, function() { add(4, 7); }); </script> Output: Approach 3: Using a Prototype in JavaScript: In this approach, we can add a method to the function constructor's prototype object. This will allow us to use the callAfter() with any function. Syntax: functionName.callAfter(delay, param1, param2, ....); Example: This example shows the above-explained approach. JavaScript <script> Function.prototype.callAfter = function(delay, ...params) { setTimeout(() => this(...params), delay); }; function add(a, b) { alert("The sum is : " + (a + b)); } add.callAfter(2000, 4, 7); </script> Output: An alert message is displayed after the specified delay for all the approached mentioned above. Comment More infoAdvertise with us Next Article How to implement a function that enable another function after specified time using JavaScript ? L lekheshwar Follow Improve Article Tags : Technical Scripter JavaScript Web Technologies Technical Scripter 2020 JavaScript-Questions +1 More Similar Reads How to Execute setInterval Function without Delay for the First Time in JavaScript? When working with JavaScript, the setInterval function is a powerful tool for executing a block of code repeatedly at specified intervals. The setInterval() method always invokes the function after the delay for the first time. Approach 1: Calling the function once before executing setInterval:Invok 2 min read How to add sleep/wait function before continuing in JavaScript? Adding a sleep/wait function in JavaScript means pausing the execution of code for a specified period. This can be done using `setTimeout` for asynchronous waits or through promises and async/await for more readable and controlled delays in asynchronous functions. Method 1: Using an infinite loop to 2 min read How to Delay a JavaScript Function Call using JavaScript ? Delaying the execution of a JavaScript function is a powerful technique often achieved using methods like setTimeout(). This approach is commonly used in over 60% of modern web applications to manage animations, control user interactions, or handle asynchronous behavior smoothly. Whether it's for de 2 min read How to call a function automatically after waiting for some time using jQuery? In order to run a function automatically after waiting for some time, we are using the jQuery delay() method. The .delay() method in jQuery which is used to set a timer to delay the execution of the next item in the queue.Syntax: $(selector).delay(para1, para2); Approach: When the Button is clicked 2 min read JavaScript Call a function after a fixed time In order to run a function multiple times after a fixed amount of time, we are using few functions. JavaScript setInterval() MethodThis method calls a function at specified intervals(in ms). This method will call continuously the function until clearInterval() is run, or the window is closed. Synta 3 min read How to call a function repeatedly every 5 seconds in JavaScript ? In JavaScript, the setInterval() method allows you to repeatedly execute a function or evaluate an expression at specified intervals. This method is particularly useful for performing periodic tasks like updating a user interface or making repeated API calls.Syntax: setInterval(function, millisecond 2 min read How to call functions after previous function is completed in jQuery ? In jQuery, to call a function after a previous function is completed, you can use callback functions. A callback ensures the second function executes only after the first one finishes, maintaining proper sequence in asynchronous operations or animations.SyntaxfunctionName();Approach: This problem ca 3 min read How to Change the Time Interval of setinterval() Method at RunTime using JavaScript ? In this article, we are going to learn how to change the setInterval() time after running one element. The setInterval() method reads the timing once and invokes the function at regular intervals. There are two methods to solve this problem which are discussed below:Table of ContentUsing clearInterv 2 min read How to create an Asynchronous function in Javascript? JavaScript is a single-threaded and synchronous language. The code is executed in order one at a time. But Javascript may appear to be asynchronous in some situations. Example: HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title 6 min read How to check if date is less than 1 hour ago using JavaScript ? Given a date and the task is to check whether the given date is less than 1 hour ago or not with the help of JavaScript. Approach 1: Count the milliseconds of the difference between the current and prev_date.If those are greater than milliseconds in 1 hour, then it returns false otherwise returns tr 2 min read Like