var http = require('http'); | |
var port = 18080; | |
http.createServer(function(req, res) { | |
var i,l = 30; | |
console.log('request received '+Date.now()); | |
res.writeHead(200, { | |
'Content-Type': 'text/html', | |
'Access-Control-Allow-Origin': '*' | |
}); | |
for(i=1;i<=l;i++){ | |
!function(i){ | |
setTimeout(function(){ | |
if(i<l){ | |
res.write('<div>The quick brown fox jumps over the lazy dog. '+i+'</div>'); | |
}else{ | |
res.end('<h1>END '+i+'</h1>'); | |
} | |
},200*i); | |
}(i); | |
} | |
}).listen(port); |
![]() |
1
Daizong 2014-05-23 19:04:39 +08:00 ![]() 一开始,我也纳闷,自己测试也是这样,不过google找到答案了,是chrome浏览器不允许同时访问同一个url,见stackoverflow:
http://stackoverflow.com/questions/15852011/why-settimeout-blocks-eventloop |
![]() |
3
xieranmaya OP @Daizong 果然是这样的,用两个浏览器测试就是正常的了
我还心说Node应该不会这样才对,原来是浏览器自己的限制,不过这个限制也是有道理的 |