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 Loop Through an Array in JavaScript? Here are the various ways to loop through an array in JavaScript1. Using for LoopThe for loop is one of the most used ways to iterate over an array. It gives you complete control over the loop, including access to the array index, which can be useful when you need to modify elements or perform other 4 min read 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 Add the Numbers in a JavaScript Array? Adding the numbers in a JavaScript array is a common operation, particularly in tasks involving data aggregation or mathematical calculations. This process involves iterating through the array and summing its elements. JavaScript provides multiple methods to achieve this, ranging from traditional lo 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 forEach() method in JavaScript ? Stopping a forEach() loop seems almost like an impossible task but here are a few tricks by which we might do it. Sample example using forEach(): var sum = 0; var number = [90, 4, 22, 48]; number.forEach(myFunction); function myFunction(item) { sum += item; } console.log(sum); Tricks to stop forEach 2 min read How to Simulate a Click in JavaScript? Simulating a click in JavaScript means triggering a click event on an HTML element using code, rather than requiring manual user interaction. This technique is useful for automating actions like clicking buttons, links, or other interactive elements, enhancing web functionality, and triggering hidde 3 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 fade-in 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 increase in the opacity it is known as the fade 4 min read Like