response.setTimeout(time,[callback]);
也可以不在setTimeout中指定回调函数,可以使用时间的监听的方式来指定回调函数.
如果没有指定超时的回调函数,那么出现超时了,将会自动关闭与http客户端连接的socket端口.如果指定了超时的回调函数,那么超时了,将会出现调用回调函数,而不会自动关闭与http客户端连接的socket端口.
复制代码 代码如下:
var http=require("http");
var server=http.createServer(function(req,res){
if(req.url!=="/favicon.ico"){
//超时监听
/*res.setTimeout(1000);
res.on("timeout",function(){
conshttp://www.cppcns.comole.log("响应超时www.cppcns.com.");
});*/
&nbshttp://www.cppcns.comp; //超时直接回调
res.setTimeout(1000,function(){
console.log("响应超时.");
});
setTimeout(function(){
res.setHeader("Content-Type","text/html");
res.write("<html><head><meta charset='utf-8' /></head>");
res.write("你好");
&nbwww.cppcns.comsp; res.end();
},2000);
}
});
server.listen(1337,"localhost",function(){
cwww.cppcns.comonsole.log("开始监听"+server.address().port+"......");
});
运行代码结果:
删除超时的回调函数后:
本文标题: node.js超时timeout详解
本文地址: http://www.cppcns.com/wangluo/javascript/116534.html
© 著作权归作者所有
举报
发表评论
0/200