I would like a add a 1-2 second delay on each iteration of the following loop.
Modern JS Solution:
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async function slowedCode() {
console.log("Before Delay")
await this.timeout(Math.random() * 2000 + 500) // Wait random amount of time between [0.5, 2.5] seconds
console.log("After Delay")
}
async function slowedForLoop() {
const data = ["1","2","3","4","5"]
for (let d of data) {
console.log(d)
await this.timeout(Math.random() * 100 + 500)
}
}
The only draw back is you have to execute the delay from inside an async function.