2011-07-31 220 views
0

我正在写谷歌浏览器扩展程序(并在学习过程中遇到很多问题)。回调函数是一个谜。我开始研究该主题以解决我发布的早期问题中的问题,并从@serg中找到包含我可以使用的模型的帖子。这里是解决方案:回调谷歌浏览器扩展

function getKeyWords(action, callback){ 
    chrome.extension.sendRequest(
      { 
       cmd: action 
      }, 
      function(response) 
      { 
       callback(response.keyWordsFound); 
      } 
    ); 
} 

var keyWords=""; 
getKeyWords("sendKeyWords", function(reply) { 
    keyWordList=reply; 

    for (var i = 0; i<keyWordList.length; ++i) 
    { 
     keyWords=keyWords+" "+keyWordList[i]; 
    } 
    msgComment1.innerHTML="<strong>"+keyWords+"</strong>"; 
    console.log("Reply is:", keyWords); 
}); 

现在我要扩展这个解决方案,但这个时候函数返回两个参数,而不是一个。我修改了我能理解的最好的代码,但它失败了。下面是修改后的代码:

function getFacePageDat(action, callback){ 
    chrome.extension.sendRequest(
      { 
       cmd: action 
      }, 
      function(response) 
      { 
       callback(response.ageList, response.seekList); 
      } 
    ); 
} 

getFacePageDat("sendSearchPageInfo", function(reply1, reply2) { 
    profileAgeCityMetro=reply1; 
    profileSeeks=reply2; 
    alert("Reply is:", profileAgeCityMetro+" seeks "+profileSeeks); 
    console.log("Reply is:", profileAgeCityMetro+" seeks "+profileSeeks); 
}); 

不幸的是失败的“错误的事件处理程序‘未定义’:类型错误:对象#的特性‘日志’是不是一个功能我知道这个问题的答案是相当简单,如果你有回调的把握,但我没有任何帮助那里

+2

你的代码看起来是正确的(除了用逗号表示的警报),问题必须在某个地方。 – serg

+0

找到错误所在的线路,然后您将知道问题所在。 –

回答

0

SERG高于可能是正确的:?

的console.log(“回复是:” profileAgeCityMetro +“寻求” + profileSeeks);

应该是

console.log(“答复是:”+ profileAgeCityMetro +“查找”+ profileSeeks);