Created
May 4, 2012 06:11
-
-
Save shenmao1989/2592480 to your computer and use it in GitHub Desktop.
测试js并发执行
This file contains hidden or 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 sleep = function(t){ | |
//console.log(+new Date()); | |
var t1 = +new Date(); | |
while(+new Date() - t1 < t){ | |
} | |
//console.log(+new Date()); | |
} | |
var f = function(x){ | |
sleep(Math.random() * 1000); | |
console.log(x); | |
} | |
for(var i = 0; i < 100; i++){ | |
setTimeout(f(i),0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
js是单线程,所以以上代码还是会顺序输出0-99