JavaScript继续标签范围

时间:2014-05-23 19:45:20

标签: javascript loops for-loop

Map.prototype.updateMap = function (vehicles) {
    nextVehicle:
    for (var i = 0; i < vehicles.length; i++) {
        for (var j = 0; j < this.oldVehicles.length; j++) {
            var vehicle = vehicles[i];
            var oldVehicle = this.oldVehicles[j];

            if (vehicle.registration == oldVehicle.registration) {
                oldVehicle.getPosition(function(latLng) {
                    if (vehicle.latitude != oldVehicle.lat) {
                        var newPos = new plugin.google.maps.LatLng(vehicle.latitude, vehicle.longitude);
                        oldVehicle.setPosition(newPos);
                    }

                    continue nextVehicle;
                });
            }
        }
    }
};

上面的代码不起作用。我觉得这与范围有关,我无法从nextVehicle方法中找到oldVehicle.getPosition标签。我怎么能绕过这个?

2 个答案:

答案 0 :(得分:1)

将匹配逻辑与更新逻辑分开。

Map.prototype.updateMap = function (vehicles) {
    // Only need to look up array lengths once
    var vehiclesLength = vehicles.length,
        oldVehiclesLength = this.oldVehicles.length; 

    for (var i = 0; i < vehiclesLength; i++) {
        var vehicle = vehicles[i];
        var oldVehicle = null;

        // Find oldVehicle
        for (var j = 0; j < oldVehiclesLength; j++) {

            if (vehicle.registration == oldVehicle[j].registration) {
                oldVehicle = oldVehicles[j];
                break;
            }
        }

        // Check for update if found
        if (oldVehicle){

            // Create closure for async callbacks
            (function(oldV, lat,lng){

                oldV.getPosition(function(latLng) {
                   if (lat != oldV.lat) {
                       var newPos = new plugin.google.maps.LatLng(lat,lng); 
                        oldV.setPosition(newPos);
                   }

                });

             })(oldVehicle, vehicle.latitude, vehicle.longitude);
        }
    }
};

答案 1 :(得分:0)

只需将continue nextVehicle;行从回调内移动到紧跟oldVehicle.getPosition(...)的调用后:

Map.prototype.updateMap = function (vehicles) {
    nextVehicle:
    for (var i = 0; i < vehicles.length; i++) {
        for (var j = 0; j < this.oldVehicles.length; j++) {
            var vehicle = vehicles[i];
            var oldVehicle = this.oldVehicles[j];

            if (vehicle.registration == oldVehicle.registration) {
                oldVehicle.getPosition(function(latLng) {
                    if (vehicle.latitude != oldVehicle.lat) {
                        var newPos = new plugin.google.maps.LatLng(vehicle.latitude, vehicle.longitude);
                        oldVehicle.setPosition(newPos);
                    }
                });

                continue nextVehicle;
            }
        }
    }
};

这假设对getPosition的调用是同步操作。

编辑:

现在,如果getPosition是异步的,则需要使用异步循环:

沿着这条线的某些东西可以解决问题:

Map.prototype.updateMap = function (vehicles) {
    var i = 0, j = -1, self = this;

    var updatePosition = function() {
        j++;

        if (j == self.oldVehicles.length) {
            j = 0;
            i++;
        }

        if (i === vehicles.length) {
            return; // We're done
        }

        var vehicle = vehicles[i];
        var oldVehicle = self.oldVehicles[j];

        if (vehicle.registration !== oldVehicle.registration) {
            updatePosition();
        }
        else {
            oldVehicle.getPosition(function(latLng) {
                if (vehicle.latitude != oldVehicle.lat) {
                    var newPos = new plugin.google.maps.LatLng(vehicle.latitude, vehicle.longitude);
                    oldVehicle.setPosition(newPos);
                    updatePosition();
                }
            });
        }
    };

    updatePosition();
};