Javascript Call for for for循环

时间:2015-10-15 11:20:49

标签: javascript arrays loops callback

我的问题是我有一个Function A在一个点调用另一个函数,我们称之为Function B(getChildContent)并需要返回值Function B才能继续。我知道这是因为Javascripts Asynchronous Nature,我试图通过回调来解决它。但我无法让它正常工作。

FunctionA(){
//some Code.....
else {
        for(i in clustertitles) {
            if(S(text).contains(clustertitles[i])) {
                var parent = {};
                parent.ClusterName = clustertitles[i];
                parent.Functions = [];
                var str = '== ' + clustertitles[i] + ' ==\n* ';
                str = S(text).between(str,'.').s;
                var caps = parseFunctions(str);
                for(y in caps) {
                    //var content = getChildContent(caps[y]);
                    getChildContent(caps[y], function(content) { //Function call
                        var child = {};
                        child.FunctionName = caps[y];
                        child.Content = [];
                        child.Content.push(content);
                        parent.Functions.push(child);       
                        console.log(content);
                    });
                }}}
}

function getChildContent (capname, callback) {
t = capname.replace(' ', '_');
bot.page(t).complete(function (title, text, date) {
    var str = S(text).between('== Kurzbeschreibung ==\n* ', '.').s;
        if(str === undefined || str === null || str === '') {
            throw new Error('Undefined, Null or Empty!');
        }
        else {
            var content = {};
            str = parseTitles(str);
            content.Owner = str[0];
            content.Aim = str[1];
            content.What = str[2];
            content.Who = str[3];
            content.Steps = str[4];
            content.Page = 'some URL';
            callback(content);
        }
});

}

所以在Function A我试图从for-Loop调用getChildContent并从caps-array传递当前字符串。对于caps-array中的每个String,getChildContent()通过node.js模块发出http请求并检索字符串。使用此字符串,我正在构建Function A中需要继续的对象(内容)。但是Function A中的'console.log(content)'只是打印出用caps-array中的最后一个字符串创建的对象,但是很多次。例如。如果caps-array有5个条目,我得到的是用caps-array的最后一个条目创建的对象的5倍。 我如何管理循环/回调以在控制台上每次都获得正确的对象?

2 个答案:

答案 0 :(得分:0)

你的循环应该调用另一个保留y值的函数,如下所示:

FunctionA(){
//some Code.....
else {
  for(i in clustertitles) {
      if(S(text).contains(clustertitles[i])) {
          var parent = {};
          parent.ClusterName = clustertitles[i];
          parent.Functions = [];
          var str = '== ' + clustertitles[i] + ' ==\n* ';
          str = S(text).between(str,'.').s;
          var caps = parseFunctions(str);
          for(y in caps) {
              yourNewFunction (y, caps, parent);
          }}}
}

function yourNewFunction (y, caps, parent) {
  getChildContent(caps[y], function(content) { //Function call
      var child = {};
      child.FunctionName = caps[y];
      child.Content = [];
      child.Content.push(content);
      parent.Functions.push(child);       
      console.log(content);
  });
}

function getChildContent (capname, callback) {
t = capname.replace(' ', '_');
bot.page(t).complete(function (title, text, date) {
    var str = S(text).between('== Kurzbeschreibung ==\n* ', '.').s;
        if(str === undefined || str === null || str === '') {
            throw new Error('Undefined, Null or Empty!');
        }
        else {
            var content = {};
            str = parseTitles(str);
            content.Owner = str[0];
            content.Aim = str[1];
            content.What = str[2];
            content.Who = str[3];
            content.Steps = str[4];
            content.Page = 'some URL';
            callback(content);
        }
});
}

答案 1 :(得分:0)

有两种方法可以做到这一点。 将循环放在函数中,在循环完成后执行回调。 (如果你在循环中进行异步调用,则会出现问题。

function doLoopdiloopStuff() {
 for() {
 }
 callback();
}

另一方面,我喜欢的方式是这样的:

for(var i = 0; i < stuff || function(){ /* here's the callback */ }(), false; i++) {
  /* do your loop-di-loop */ 
}

在另一个例子中:

for (var index = 0; index < caps.length || function(){ callbackFunction(); /* This is the callback you are calling */ return false;}(); index++) {
            var element = caps[index];
            // here comes the code of what you want to do with a single element
}