How to add a delay in a JavaScript loop? Last Updated : 31 May, 2024 Comments Improve Suggest changes Like Article Like Report JavaScript doesn't offer any wait command to add a delay to the loops but we can do so using setTimeout method. This method executes a function, after waiting a specified number of milliseconds. Below given example illustrates how to add a delay to various loops: For loop: JavaScript for (let i=0; i<10; i++) { task(i); } function task(i) { setTimeout(function() { // Add tasks to do }, 2000 * i); } In the code given above you have to do 2000 * i at line 8 because setTimeout method inside the loop doesn't makes the loop pause but actually adds a delay to each iteration. Remember that all the iteration start their time together. Thus if we only do 2000 there, that will make all the iterations execute together and it will just give 2000 ms delay in the first iteration and all other iteration will happen instantly after it. Thus to avoid that we add 0 to first, 2000 to second, 4000 to third and it goes on. Example: Below given program will print 0 to 9 in console after 2 seconds delay to each number using for loop. JavaScript <script> for (let i=0; i<10; i++) { task(i); } function task(i) { setTimeout(function() { console.log(i); }, 2000 * i); } </script> Output: While loop: Same concept is applied to make below given while loop. JavaScript let i = 0; while (i < 10) { task(i); i++; } function task(i) { setTimeout(function() { // Add tasks to do }, 2000 * i); } Example: Below given program will print 0 to 9 in console after 2 seconds delay to each number using while loop. JavaScript <script> let i = 0; while (i < 10) { task(i); i++; } function task(i) { setTimeout(function() { console.log(i); }, 2000 * i); } </script> Output: Do-while loop: Same concept is applied to make below given do-while loop. JavaScript let i = 0; do { task(i); i++; } while (i < 5); function task(i) { setTimeout(function() { // Add tasks to do }, 2000 * i); } Example: Below given program will print 0 to 9 in console after 2 seconds delay to each number using do-while loop. JavaScript <script> let i = 0; do { task(i); i++; } while (i < 5); function task(i) { setTimeout(function() { console.log(i); }, 2000 * i); } </script> Output: Approach 4: Using async/await with setTimeoutES6 introduced async functions which enable the use of await, which suspends the execution of an async function until the passed promise resolves. We can utilize this feature along with setTimeout to add delays to loops. JavaScript const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)); const printNumbersWithDelay = async () => { for (let i = 0; i < 10; i++) { await delay(2000); // wait for 2 seconds console.log(i); } }; printNumbersWithDelay(); Output: Comment More infoAdvertise with us Next Article How to add a delay in a JavaScript loop? gurrrung Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Delay a Function Call in JavaScript ? Delaying a JavaScript function call involves executing a function after a certain amount of time has passed. This is commonly used in scenarios where you want to postpone the execution of a function, such as in animations, event handling, or asynchronous operations. Below are the methods to delay a 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 delay a loop in JavaScript using async/await with Promise ? In JavaScript, you can delay a loop by using async/await with Promise. By wrapping a setTimeout() inside a Promise and using await, you can pause execution at each iteration, creating delays between loop iterations for asynchronous tasks without blocking the main thread.What is async and await?async 2 min read How to use async/await with forEach loop in JavaScript ? Asynchronous is popular nowadays because it gives functionality of allowing multiple tasks to be executed at the same time (simultaneously) which helps to increase the productivity and efficiency of code. Async/await is used to write asynchronous code. In JavaScript, we use the looping technique to 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 add time delay in Python? In this article, we are going to discuss how to add delay in Python. How to add Time Delay?In order to add time delay in our program code, we use the sleep() function from the time module. This is the in-built module in Python we don't need to install externally.Time delay means we are adding delay 5 min read What is An Event Loop in JavaScript? The event loop is an important concept in JavaScript that enables asynchronous programming by handling tasks efficiently. Since JavaScript is single-threaded, it uses the event loop to manage the execution of multiple tasks without blocking the main thread.JavaScriptconsole.log("Start"); setTimeout( 4 min read How to Pause and Play a Loop in JavaScript using Event Listeners ? Given below is a JavaScript program for DOM manipulation which is basically about How to Pause and Play a loop in JavaScript using event listeners (not to be confused with delay). In this article, we are using JavaScript with HTML so we need a web browser i.e., Chrome (recommended) or Electron Appli 4 min read How to Create an Observable with a Delay in TypeScript ? Creating observables with delays is a common requirement in TypeScript applications, especially when dealing with asynchronous operations. To achieve this, we leverage the power of the rxjs library. What is rxjs library?The RxJS library is a powerful reactive programming library for JavaScript and T 3 min read How to add fade-out effect using pure JavaScript ? The fade effect is described as the gradual increase and decrease in the opacity of the selected portion. In other words, the fade effect is termed as the increase and decrease in opacity with respect to time. When this effect is applied with a gradual decrease in the opacity it is known as the fade 4 min read Like