callbacks google chrome extension
I am writing a Google Chrome Extension (and learning an awful lot in the process). Callback functions are something of a mystery. I started researching the subject to solve a problem from an earlier question I posted and found a post from @serg containing a model I could use. Here is the solution:
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);
});
Now I want to extend this solution but this time the function has to return two arguments instead of one. I modified the code above the best I could understand it but it fails. Here is the modified code:
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);
});
Unfortunately this fails on "Error in event handler for 'undefined': TypeError: Property 'log' of object # is not a function. I know the answer to this question is rather simple if you hav开发者_如何学运维e a grasp of callbacks but I don't. Any help out there?
Serg is likely correct above:
console.log("Reply is:", profileAgeCityMetro+" seeks "+profileSeeks);
should be
console.log("Reply is:" + profileAgeCityMetro + " seeks " + profileSeeks);
精彩评论