Javascript Closure内部循环

时间:2015-09-02 16:56:17

标签: javascript

window.config = {
"Environments": [
    "LH5",
    "LH8",
    "AMS"
],
"Clusters": [
    4,
    4,
    4
]
};

以下是承诺对象:

for (var i = 0; i < window.config.Environments.length; i++) {

    for (var j = 1; j < window.config.Clusters[i] + 1; j++) {

      promiseObj.physical[window.config.Environments[i] + "#Cluster" + j] = $http.get('http://url0/search?idc=' + window.config.Environments[i] + '&type=Physical&cluster=' + j).success(function(data) {


            $scope.servers = data; // get data from json

            countcores[window.config.Environments[i] + "#Cluster" + j] = 0;
            countmemory[window.config.Environments[i] + "#Cluster" + j] = 0;

            angular.forEach($scope.servers, function(item) {

                countcores[window.config.Environments[i] + "#Cluster" + j] = parseInt(countcores[window.config.Environments[i] + "#Cluster" + j]) + parseInt(item.cores);
                countmemory[window.config.Environments[i] + "#Cluster" + j] = parseInt(countmemory[window.config.Environments[i] + "#Cluster" + j]) + parseInt(item.memory);

            });


        });

           promiseObj.virtual[window.config.Environments[i] + "#Cluster" + j] = $http.get('http://url/search?idc=' + window.config.Environments[i] + '&type=Virtual&cluster=' + j).success(function(data) {


            $scope.servers = data; // get data from json

            countvirtualcores[window.config.Environments[i] + "#Cluster" + j] = 0;
            countvirtualmemory[window.config.Environments[i] + "#Cluster" + j] = 0;


            angular.forEach($scope.servers, function(item) {

                countvirtualcores[window.config.Environments[i] + "#Cluster" + j] = parseInt(countvirtualcores[window.config.Environments[i] + "#Cluster" + j]) + parseInt(item.cores);
                countvirtualmemory[window.config.Environments[i] + "#Cluster" + j] = parseInt(countvirtualmemory[window.config.Environments[i] + "#Cluster" + j]) + parseInt(item.memory);

            });


        });

}
}

似乎正在发生的事情是循环比promise对象更快,j在promise对象之前达到5并且记录的是

["undefined#Cluster5"] = 1280

我期待的是

["LH5#Cluster1"] = somevalue;
["LH5#Cluster2"] = somevalue;
["LH5#Cluster3"] = somevalue;
["LH5#Cluster4"] = somevalue;

["LH8#Cluster1"] = somevalue;
["LH8#Cluster2"] = somevalue;
["LH8#Cluster3"] = somevalue;
["LH8#Cluster4"] = somevalue;

["AMS#Cluster1"] = somevalue;
["AMS#Cluster2"] = somevalue;
["AMS#Cluster3"] = somevalue;
["AMS#Cluster4"] = somevalue;

我有多个promiseObjectives在同一个循环中运行 - 这是如何工作的?我知道JavaScript closure inside loops – simple practical example - 但这对我没有帮助,我需要进一步的帮助。

1 个答案:

答案 0 :(得分:1)

引用的URL旨在以简单的方式举例说明您需要做的事情。您遇到的问题与URL中引用的问题相同。您需要确保自己确定i&amp;每个函数调用的j个变量。因此,您需要围绕成功函数关闭i&amp; j变量,用于在success()函数调用$http.get()时返回您要调用的函数。

我不打算执行确切的代码,但它看起来有点类似:

$http.get('http://url').success(function(i,j){
    return function (data){
        //code stuff happens here
    }
}(i,j));

这样调用function(i,j){ ... }(i,j)然后它会返回return function(data){}i的实际函数jfunction(i,j){ ... }(i,j)变量适当确定范围并具有调用函数i时所执行的值。

**作为旁注,我会更改j&amp;的范围可变名称。 - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView { return [self.items count]; } - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { Item *item = [self.items objectAtIndex:row]; NSString *identifier = [tableColumn identifier]; NSView *result = nil; if ([identifier isEqualToString:@"thumb"]) { NSImageView *thisCell = [tableView makeViewWithIdentifier:identifier owner:self]; NSString *url = [item valueForKey:@"url"]; NSImage *image = [[NSImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]]; [thisCell setImage:image]; result = thisCell; } else { NSString *value = [item valueForKey:identifier]; NSTableCellView *thisCell = [tableView makeViewWithIdentifier:identifier owner:self]; thisCell.textField.stringValue = value; result = thisCell; } return result; } -(IBAction)add:(id)sender { Item *testItem = [[Item alloc] init]; testItem.title = @"Some Title"; testItem.url = @"http://filmdash.files.wordpress.com/2010/02/sayhello_logo_300dpi_rgb.jpg"; [self.items addObject:testItem]; [self.tableView reloadData]; } 更具描述性和不那么令人困惑的内容