-3
我有要求调用循环内的函数。我设法写了一个闭包函数。但它不适合我。请帮助解决这个问题。非常感谢您的帮助。Javascript调用循环内的函数
for(var i = 0; i < pointsData.length; i++){
(function(foo){
routeId = pointsData[i]._id;
colorId = pointsData[foo].route_color;
console.log('color start', colorId);
$.post('commands.php',{command: 'get-route-coords',id: pointsData[foo]._id}
,function(data){
MYMAP.setAllRoutes(last, coordinates, pointsData[foo].route_color);
});
}(i));
}
MYMAP.setAllRoutes = function(center,flightPlanCoordinates,colorId) {
console.log('color ids', colorId);
}
什么不起作用?你的问题需要更多细节。有错误吗? – Andy 2014-10-09 11:38:14
你在你的函数中使用'foo'(明确的名字!)和'i'。你没有声明'routeId','colorId'作为变量,所以它们是全局变量......但无论如何,你忘了在之后使用它们。您的每行比例接近一个错误=>您需要睡觉,或了解更多关于Javascript的信息;-)。 – GameAlchemist 2014-10-09 11:52:48