Answers for "set timeout for loop"

1

set timeout for loop

window.purls = [11213,23121,43343,5644,77535]
i = 0;
for (i = 0; i < window.purls.length; i++){
    doSetTimeout(i);
}

function doSetTimeout(i) {
	setTimeout(function() {
		// Code goes here
	}, i*1200); //1200 = time in milliseconds
}
Posted by: Guest on October-13-2021
2

settimeout in loop javascript

var array = [1, 2, 3, 4, 5]for(var i = 0; i < array.length; i++) {  setTimeout(() => {    console.log(array[i])  }, 1000);} // i = 5
Posted by: Guest on August-06-2020
0

settimeout inside loop

//you can leave the sleep constant
const sleep = (milliseconds) => {
  return new Promise(resolve => setTimeout(resolve, milliseconds))
}

const doSomething = async () => {
  for (/*for loop statements here*/) {
  //code before sleep goes here, just change the time below in milliseconds
   await sleep(1000)
  //code after sleep goes here 
    }
  }
doSomething();
Posted by: Guest on October-04-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language