Skip to content

Instantly share code, notes, and snippets.

@wulymammoth
Last active October 27, 2015 16:11
Show Gist options
  • Select an option

  • Save wulymammoth/bf53b0b68dab6f254fbd to your computer and use it in GitHub Desktop.

Select an option

Save wulymammoth/bf53b0b68dab6f254fbd to your computer and use it in GitHub Desktop.
for (var i = 0; i < 5; i++) {
setTimeout(function() {
console.log(i);
}, 1000);
}
// How do we fix the above to log what we want it to log?
for (var j = 0; j < 5; j++) {
// With an IIFE (immediately-invoked function expression)
// Closures
(function(j) {
setTimeout(function() {
console.log(j);
}, 1000);
})(j);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment