自循环函数超过node.js调用堆栈
这段代码获取URL,但我的服务器无法同时处理1k请求,所以我将其重新设置为给定值,但它返回以下错误,为什么自循环函数超过node.js调用堆栈,node.js,recursion,Node.js,Recursion,这段代码获取URL,但我的服务器无法同时处理1k请求,所以我将其重新设置为给定值,但它返回以下错误,为什么 var http = require('http'), path = '/getter.php?id='; options = { host : 'localhost', port : 80, path : '/getter.php?id=', method : 'GET', rejectUn
var http = require('http'),
path = '/getter.php?id=';
options = {
host : 'localhost',
port : 80,
path : '/getter.php?id=',
method : 'GET',
rejectUnauthorized: false,
requestCert: true,
agent: false
},
get = function (i, max, step, time) {
for (i; i <= max; i++) {
var handler = function (key) {
http.request(options, function(res) {
console.log(key);
}).end();
};
options.path = path + i;
}
setTimeout(get(i + step, max + step), time);
};
get(0, 10, 10, 1000);
这是导致问题的呼叫:
setTimeout(get(i + step, max + step), time);
这是因为您正在调用get,而不是将其设置为稍后调用
相反,将函数和参数传递给setTimeout
:
setTimeout(get, time, i + step, max + step);
从节点(添加了强调):
计划在延迟毫秒后执行一次性回调。
返回一个timeoutObject,以便与clearTimeout()一起使用。
您还可以选择将参数传递给回调。
setTimeout(get, time, i + step, max + step);