Skip to content

Instantly share code, notes, and snippets.

@yangjunjun
Created November 1, 2013 10:54
Show Gist options
  • Save yangjunjun/7263840 to your computer and use it in GitHub Desktop.
Save yangjunjun/7263840 to your computer and use it in GitHub Desktop.
var len = 4;
while(len--){
setTimeout(function(){
alert(len);
},0);
alert(len);
}
// out: 3,2,1,0,-1,-1,-1,-1
var len = 4;
while(len--){
(function(i){
setTimeout(function(){
alert(i + ' inner');
},0);
})(len);
alert(len);
}
// out: 3,2,1,0,3,2,1,0 (后四个顺序不确定)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment