【发布时间】:2014-07-11 11:49:38
【问题描述】:
我一直在努力解决这个问题
var http = require('http'),
parseString = require ('xml2js').parseString;
for (var noOfZones = 1; noOfZones<=8; noOfZones++) {
var newPath = 'http://192.168.1.200/getZoneData?zone='+noOfZones;
var req = http.request(newPath, function(zres, noOfZones) {
zres.setEncoding('utf8');
zres.on('data', function (zonedata) {
parseString(zonedata, {
trim:true,
childkey:9,
explicitArray:false,
explicitChildren:false
}, function (err, zones) {
console.log(noOfZones);//this one bring back undefined
});
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message)
})
req.end();
}
for 循环可以将 noOfZones 传递给 newPath。但是,该值未在函数内传递(错误,区域) 我一直在阅读有关将变量传递给内联函数的内容,并试图将其放置在各个级别,但它似乎对我不起作用。我得到的只是未定义的。 (即使我把它串起来)
【问题讨论】:
-
Google 搜索“臭名昭著的循环问题 javascript”
标签: javascript function for-loop inline