So I know that setTimeout() function can trigger the function passed in after a certain period of time, but it seems behaving differently in a loop. For example:
for (var i = 0; i < 5; i++) {
setTimeout(console.log(i + ', time is: ' + new Date()), 1000);
}
The console.log() seems printing out the numbers all at the same time. I do not understand why. Could some of you explain why setTimeout()
does not work in this loop?