-3

Why is setTimeout running 10 times in a loop at once, instead of waiting one second, then running, waiting one second and running etc..

const tx = document.querySelector('.botex')

console.log(tx.lastElementChild.className)

for(let i =0;i<10;i++) {
    setTimeout(() => {
    let msg = document.createElement('p');
    msg.textContent = "Hello there" + i;
    tx.appendChild(msg);
  }, 1000)

}
<div class="botex">
<div class="1">

</div>
<div class="2">

</div>
</div>
squnk
  • 318
  • 1
  • 8

0 Answers0