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//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();
Also in JavaScript:
- react native image fit container
- on enter key press react
- check for balanced parentheses in javascript
- gradle json simple dependency
- javascript Count the frequency of a value in an array
- js for each item do
- angular component lifecycle hooks
- button click function in js
- span change jquery
- diff two arrays javascript
- loopback not null
- how to add two attay into object in javascript
- how to change the font family using jquery
- smooth scrolll to id js
- rorate array
- Module Error (from ./node_modules/eslint-loader/dist/cjs.js):
- jquery table row calculation
- (!+[]+[]+![]).length 9
- ejs comments
- javascript convert date from mm/dd/yyyy to yyyymmdd
- asp net core use newtonsoft json
- expressjs swagger yaml
- javascript stringify line breaks
- datatables hide showing entries