那是因爲你沒有提供回調。
function doSomethingLater(callback) {
setTimeout(callback, 1000);
}
console.log('This is before the callback');
doSomethingLater(function() {
console.log('This is the callback')
});
所以,當你調用var gaiaId = getGaia();
你是不是傳遞一個回調函數
[編輯]這是你的代碼需要什麼樣子:
function getGaia(callback) {
chrome.identity.getProfileUserInfo(function(userInfo){
var userId = userInfo.id;
// This will call the function that you pass in below
//and pass in userInfo.if as a parameter
callback(userInfo.id);
});
}
var gaiaId = getGaia(function (id) {
// id === userInfo.id from above
// Do something with the id that you pass in
});
你可以想像JavaScript中的變量這樣的函數,
所以,你可以指定一個函數來像這樣的變量:
var foo = function() { ... }
這意味着你可以傳遞到像正常變量函數這一點。當你傳遞給函數作爲一個參數,要指定您在參數中指定的函數名稱:
var foo = function() { ... }
function hasCallback(callback) {
// The following two line do exactly the same thing:
callback(); // Using the function that you passed in
foo(); // Using the function directly
}
hasCallback(foo);
所有我在上面做,而不是創建變量foo
我剛剛創建的功能內聯:
var foo = function() { ... }
function hasCallback(callback) {
// The following two line do exactly the same thing:
callback(); // Using the function that you passed in
foo(); // Using the function directly
}
hasCallback(foo);
// Becomes:
function hasCallback(callback) {
callback(); // Using the function that you passed in
}
hasCallback(function() { ... });
您確定使用'callback'或'Callback'嗎?該錯誤表示您正在使用「回撥」。但是你的代碼有小寫回調。 – zero298
錯誤,'回調'是不是一個函數,在這裏C是回調的資本 –
已編輯 - 這是一個錯字 – JamesJameson2456