Created
November 1, 2013 10:54
-
-
Save yangjunjun/7263840 to your computer and use it in GitHub Desktop.
参见:http://segmentfault.com/q/1010000000328849 http://heroicyang.com/2012/08/28/javascript-event-loop/
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
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