settimeout javascript in for loop code example
Example 1: settimeout inside loop
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
const doSomething = async () => {
for () {
await sleep(1000)
}
}
doSomething();
Example 2: settimeout in a for loop javascript
for (let i = 0; i < 5; i++) {
setTimeout(() => console.log(i), 0);
}
Example 3: 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);}