尝试在JavaScript中执行函数时遇到了一些逻辑问题。基本上我有一个pointArr来存储沿路线的坐标。然后我得到一个moveNext(),它接收沿着路线的每个坐标并绘制到地图上。然后在moveNext()内部,我得到另一个数组,它是busList。如果沿着路线的坐标与busList的坐标匹配,那么我将totalBusStopLeft减去一。这是我调用moveNext()的代码:
getAllBusLoc(function(busList) {
//At first I set the totalBusLeft by the length of busList which is 13 in this case
var totalBusLoc = busList.length;
document.getElementById("busStopLeft").innerHTML = totalBusLoc;
//Then I set the value to busList for the minus calculation
busLeft = totalBusLoc;
timeout = 1500;
pointArr.forEach(function(coord,index){
setTimeout(function(){
moveNext(coord.x, coord.y, index, busList, busLeft);
}, timeout * index);
});
});
function moveNext(coordx, coordy, k, busList, busLeft){
document.getElementById("busStopLeft").innerHTML = busLeft;
//pointToCompare is the coordinates in the route but not the coordinates of busList
var pointToCompare = coordx + "," + coordy;
//If the coordinates in route matches the coordinate in busList, I minus busLeft by one
if(busList.indexOf(pointToCompare) > -1){
parseFloat(busLeft--);
}
//Code to Add marker
}
但是,使用此代码,我的Html组件busStopLeft继续显示13,即totalBusLoc,但不是busLeft。有任何想法吗?
提前致谢。
修改
totalBusLoc = busList.length;
document.getElementById("busStopLeft").innerHTML = totalBusLoc;
timeout = 1500;
pointArr.forEach(function(coord,index){
setTimeout(function(busLeft){
moveNext(coord.x, coord.y, index, busList, totalBusLoc);
}, timeout * index);
});
});
function moveNext(coordx, coordy, k, busList, totalBusLoc, callback){
var pointToCompare = coordx + "," + coordy;
if(busList.indexOf(pointToCompare) > -1){
parseFloat(totalBusLoc--);
document.getElementById("busStopLeft").innerHTML = totalBusLoc;
callback(totalBusLoc);
}
}