Skip to content

Instantly share code, notes, and snippets.

@shenmao1989
Created May 4, 2012 06:11
Show Gist options
  • Save shenmao1989/2592480 to your computer and use it in GitHub Desktop.
Save shenmao1989/2592480 to your computer and use it in GitHub Desktop.
测试js并发执行
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);
}
@shenmao1989
Copy link
Author

js是单线程,所以以上代码还是会顺序输出0-99

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment