0

我不太了解回调函数,这可能是我遇到问题的根源:我有一个 getCookies() 函数并且它有效,但现在我将它移到了 background.js 中,函数外的变量是仍然未定义。

看看:

function getCookies(domain, name, callback) {
    chrome.cookies.get({"url": domain, "name": name}, function(cookie) {
        if(callback) {
            callback(cookie ? cookie.value : null);
        }
    });
}

var upw; //Passwort HASH


//USER PW Hash auslesen
    getCookies("http://example.org/", "upw", function(id) {

    if(id == null) { 
        upw= null;
        }
    else { upw = id;}
    console.log("Func: "+upw);
});

console.log("Outside: "+upw);

控制台会给我这样的东西:

Outside: undefined
Func: 1234asdfqwertz5678

因此,该功能将“为时已晚”执行。我不知道为什么,也不知道如何解决这个问题!

4

1 回答 1

0

回调函数是异步的,所以不能工作!我正在处理回调函数中的所有内容。

于 2013-10-05T19:07:32.630 回答