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? 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 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 Set Time Delay in JavaScript? Delaying the execution of code is a fundamental technique that is commonly used in JavaScript for tasks like animations, API polling, or managing time intervals between actions. JavaScript provides several built-in methods to set time delays: setTimeout() and setInterval(). We can set time delay in 2 min read How to stop setInterval Call in JavaScript ? In JavaScript, the setInterval() function is used to repeatedly execute a specified function at a fixed interval. However, there may be scenarios where we need to stop the execution of setInterval() calls dynamically. Stopping a setInterval() call in JavaScript is essential to prevent ongoing repeti 2 min read How to run a function when the page is loaded in JavaScript ? A function can be executed when the page loads successfully. This can be used for various purposes like checking for cookies or setting the correct version of the page depending on the user's browser. Below are the approaches to run a function when the page is loaded in JavaScript: Table of Content 2 min read How to clear setInterval without knowing the ID in jQuery ? In this article, we are going to learn how can we clear setInterval without knowing its ID using jQuery. First of all, let's briefly understand about setInterval. This method is used for executing any specific function or some section of code at a particular period of time So that we don't have to u 3 min read Like