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:
for(let i=0; i<10; i++) {task(i);}functiontask(i) {setTimeout(function() {// Add tasks to do}, 2000 * i);}chevron_rightfilter_noneIn 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.
<script>for(let i=0; i<10; i++) {task(i);}functiontask(i) {setTimeout(function() {console.log(i);}, 2000 * i);}</script>chevron_rightfilter_noneOutput:
- While loop: Same concept is applied to make below given while loop.
let i = 0;while(i < 10) {task(i);i++;}functiontask(i) {setTimeout(function() {// Add tasks to do}, 2000 * i);}chevron_rightfilter_noneExample: Below given program will print 0 to 9 in console after 2 seconds delay to each number using while loop.
<script>let i = 0;while(i < 10) {task(i);i++;}functiontask(i) {setTimeout(function() {console.log(i);}, 2000 * i);}</script>chevron_rightfilter_noneOutput:
- Do-while loop: Same concept is applied to make below given do-while loop.
let i = 0;do{task(i);i++;}while(i < 5);functiontask(i) {setTimeout(function() {// Add tasks to do}, 2000 * i);}chevron_rightfilter_noneExample: Below given program will print 0 to 9 in console after 2 seconds delay to each number using do-while loop.
<script>let i = 0;do{task(i);i++;}while(i < 5);functiontask(i) {setTimeout(function() {console.log(i);}, 2000 * i);}</script>chevron_rightfilter_noneOutput:
Recommended Posts:
- How to execute setInterval function without delay for the first time in JavaScript ?
- 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
- JavaScript | While Loop
- JavaScript | For Loop
- How to use loop through an array in JavaScript?
- How to break nested for loop using JavaScript?
- How to trigger setInterval loop immediately using JavaScript ?
- How to ignore loop in else condition using JavaScript ?
- JavaScript for-in Loop
- What are the microtask and macrotask within an event loop in JavaScript ?
- JavaScript SyntaxError - A declaration in the head of a for-of loop can't have an initializer
- Atomics.add( ) In JavaScript
- How to add an object to an array in JavaScript ?
- JavaScript weakSet.add() Method
- JavaScript | Add new elements at the beginning of an array
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.


