for (var i = 1; i <= 4; i++) {
(function (j) {
setTimeout(function () {
console.log(j);
}, j * 1000);
})(i);
}
In the above code my understanding is that each iteration of the for loop, the time before the IIFE executes increases since it's multiplying the current item in the loop by 1000; it will be 1 second, 2 seconds, 3 seconds, then 4 seconds before it executes. But when I try running this in the Chrome developer tools console it doesn't seem like the amount of time between 1, 2, 3, and 4 being printed is increasing at all. It's definitely not 4 seconds between 3 and 4 being printed.
To compare try running:
(function (j) {
setTimeout(function () {
console.log(j);
}, j * 1000);
})(4);
Am I missing something here?