How to Execute setInterval Function without Delay for the First Time in JavaScript? Last Updated : 14 Oct, 2024 Comments Improve Suggest changes Like Article Like Report 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:Invoke the function once before using setInterval() for immediate execution.Set up the setInterval() function with the desired callback after the immediate invocation.Create a new function that first invokes the original function, then calls setInterval().This method simulates the setInterval() behavior without a delay for the first execution.Example: This example shows the implementation of the above-mentioned approach. html <!DOCTYPE html> <html> <head> <title> How to execute setInterval function without delay for the first time in JavaScript ? </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> Execute the setInterval function without delay the first time </b> <p> Click on the button to execute the setInterval() function without delay. </p> <button onclick="startSetInterval()"> Start immediate setInterval </button> <script type="text/javascript"> let count = 1; function exampleFunction() { console.log('Function Executed! ' + count); count = count + 1; } function noDelaySetInterval(func, interval) { func(); return setInterval(func, interval); } function startSetInterval() { noDelaySetInterval(exampleFunction, 3000); } </script> </body> </html> Output:Immediately after clicking the button:After waiting for 3 seconds:Method 2: Using an immediately invoking function inside setInterval function An Immediately-invoked Function Expression (IIFE) is a function that executes immediately after its declaration.IIFEs can be used as callbacks in the setInterval() function to ensure immediate execution.This approach allows the IIFE to run once before the actual setInterval() begins its execution after the specified delay.Using an IIFE simulates setInterval() behavior without a delay for the first invocation.Example: This example shows the implementation of the above-mentioned approach. html <!DOCTYPE html> <html> <head> <title> Execute the setInterval function without delay the first time </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> Execute the setInterval function without delay the first time </b> <p> Click on the button to execute the setInterval() function without delay. </p> <button onclick="startSetInterval()"> Start immediate setInterval </button> <script type="text/javascript"> function startSetInterval() { let count = 1; setInterval(function exampleFunction() { console.log('Function Executed! ' + count); count = count + 1; return exampleFunction; }(), 3000); } </script> </body> </html> Output:Immediately after clicking the button:After waiting for 3 seconds: Comment More infoAdvertise with us Next Article How to Execute setInterval Function without Delay for the First Time in JavaScript? S sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to measure time taken by a function to execute using JavaScript ? This article will show how to measure the time taken by a function to execute using Javascript. To measure the time taken by a function to execute we have three methods: Table of Content Using the Using Date ObjectUsing the performance.now() methodUsing the console.time() methodMethod 1: Using the U 3 min read Difference Between setTimeout & setInterval in JavaScript JavaScript has both setTimeout and setInterval functions that are used for executing code after a certain delay. However, they differ in how they handle the timing of execution. Understanding their differences is crucial for effectively managing asynchronous operations in our code which is explained 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 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 implement a function that enable another function after specified time using JavaScript ? 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: 2 min read Like