Answers for "can you delay each iteration of a loop in javascript"

3

how to delay iterations in javascript

var i = 1;                  //  set your counter to 1

function myLoop() {         //  create a loop function
  setTimeout(function() {   //  call a 3s setTimeout when the loop is called
    console.log('hello');   //  your code here
    i++;                    //  increment the counter
    if (i < 10) {           //  if the counter < 10, call the loop function
      myLoop();             //  ..  again which will trigger another 
    }                       //  ..  setTimeout()
  }, 3000)
}

myLoop();                   //  start the loop
Posted by: Guest on October-09-2020
5

adding delay in javascript foreach loop

// tasks is your array
tasks.forEach((element,i) => {
                setTimeout(
                    function(){
                     	//the work you want to perform   
                    }
                , i * 300);
});
Posted by: Guest on April-26-2020

Code answers related to "can you delay each iteration of a loop in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language