Created
March 26, 2017 15:10
-
-
Save llimacruz/dc661bb640b14075ce0406a650a7f0cf to your computer and use it in GitHub Desktop.
Assinc for in js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// does not work | |
for (var i = 1; i <5; i++) { | |
setTimeout(function() { | |
console.log('i: ' + i); | |
}, i * 1000); | |
} | |
// work | |
for (var i = 1; i <5; i++) { | |
let j = i; | |
setTimeout(function() { | |
console.log('i: ' + i + ' j: ' + j); | |
}, i * 1000); | |
} | |
// work | |
for (var i = 1; i <5; i++) { | |
(function(i) { | |
setTimeout(function() { | |
console.log('i: ' + i); | |
}, i * 1000); | |
})(i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment