'JavaScript SetTimeout event loop latency problems

For example,

for (var i = 0; i < 3; i++) {
  setTimeout(function() {
    console.log(i)
  }, 500);

  let j = i;
  setTimeout(function() {
    console.log(j);
  }, 1000)
}

The event loop loupe result: 3 0 3 1 3 2

But in jsfiddle result: 3 3 3 0 1 2

I dont know what cause the two different results and which one would be the correct one.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source