The setInterval() method always invokes the function after the delay for the first time using two approaches:
Method 1: Calling the function once before executing setInterval: The function can simply be invoked once before using the setInterval function. This will execute the function once immediately and then the setInterval() function can be set with the required callback.
A new function is created which encloses the invoking of the function first and then calls the setInterval() function next. This will help to simulate the setInterval() function without a delay for the first time.
Example:
<!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 type of function that immediately gets invoked after its declaration. This property can be used in the callback of the setInterval() function, as it would get immediately executed once and then the actual setInterval() with this function will start after the specified delay. This will help to simulate the setInterval() function without a delay for the first time.
Example:
<!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:
Recommended Posts:
- How to Change the Time Interval of setinterval() Method at RunTime using JavaScript ?
- How to trigger setInterval loop immediately using JavaScript ?
- How to measure time taken by a function to execute using JavaScript?
- Java Script | setTimeout() & setInterval() Method
- How to add a delay in a JavaScript loop?
- How to redirect to multiple websites with a delay using JavaScript ?
- Dynamically Execute JavaScript in ElectronJS
- How to store JavaScript functions in a queue and execute in that order?
- CSS | animation-delay Property
- jQuery | delay() with Examples
- CSS | transition-delay Property
- Underscore.js | _.delay() with Examples
- Lodash _.delay() Method
- p5.js Image delay() Method
- How to execute AngularJS controller function on page load ?
- How to execute PHP code using command line ?
- How to execute TypeScript file using command line?
- How to compare date part only without comparing time in JavaScript?
- Delete the first element of array without using shift() method in JavaScript
- How to convert UTC date time into local date time using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


