2015-10-15 123 views
0

我的問題是,我有一個Function A調用另一個函數,我們稱之爲Function B(getChildContent)並需要返回值Function B才能繼續。我知道這是因爲JavaScript異步自然,我試圖用回調來解決它。但我無法正常工作。Javascript回調for循環

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循環調用getChildContent,並通過從蓋陣列當前字符串。對於caps-array中的每個字符串,getChildContent()通過node.js模塊發出http請求並檢索字符串。有了這個字符串,我正在構建一個需要在Function A中繼續的對象(內容)。然而,Function A中的'console.log(content)'只是打印出用caps-array中的最後一個字符串創建的對象,但是多次。例如。如果caps-array有5個條目,我得到5次用caps-array的最後一個條目創建的對象。 我如何管理循環/回調以獲得每次控制檯上正確的對象?

+0

這億韓元當你的回調函數被調用時,你的y變量已經改變了它的值 –

+0

你已經嘗試了'Function.prototype.bind'嗎? – klenium

+0

@ EugenTimm我知道它不工作,你有沒有提示它工作? – dnks23

回答

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); 
     } 
}); 
} 
+0

感謝您的幫助,但我通過調用另一個函數,因爲你建議上述.... – dnks23

0

有2種方式來做到這一點。 將循環放入函數中,循環完成後執行回調。內環路(有問題的,如果你正在做的異步調用

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 
} 
+0

謝謝爲你的幫助,但即時通訊努力將你的提示轉換爲我的例子....在我上面的'東西'的例子將是'caps.length'對嗎? '||'之後的部分對我來說不太清楚...... – dnks23

+0

是的。東西是你的帽子。 「||」是或以這種形式。所以是通過所有你的項目大寫。之後它進入功能。在函數內部,你可以建立你的回調。返回值或從中調用另一個函數。 – RenokK

+0

好吧,我不能得到它的工作......我所做的只是簡單地在'||'之後編寫getChildContent函數,在循環....但它不工作....你是什麼意思'你可以在這裏建立你的回調'..... – dnks23