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);
});
不幸的是失敗的「錯誤的事件處理程序‘未定義’:類型錯誤:對象#的特性‘日誌’是不是一個功能我知道這個問題的答案是相當簡單,如果你有回調的把握,但我沒有任何幫助那裏
你的代碼看起來是正確的(除了用逗號表示的警報),問題必須在某個地方。 – serg
找到錯誤所在的線路,然後您將知道問題所在。 –