I am running a while loop which prints the number of iterations of the loop one after another each in one console line.
<script>
var i = 0;
while (i < 10) {
console.log("The number is " + i);
i++;
}
</script>
However, I would like to introduce a pause of 1 second between each console.log
output. I searched on the internet and stackoverflow for solutions and came across setTimeout()
.
<script>
var i = 0;
while (i < 10) {
setTimeout(function (){console.log("The number is " + i)}, 1000);
i++;
}
</script>
But when I introduce setTimeout()
into my code, instead of printing each number with a pause of 1 second inbetween into a seperate console line, after one second the number 10 is printed.