我有一个字符串需要在多个函数中使用,因此我想将它保存在一个变量中。
但是当我尝试在函数内部赋值时,它不会更新该变量。
var auth_code = "na";
function safeAuthCode(authcode){
auth_code = authcode;
console.log(auth_code);
}
"
auth_code
"在控制台上能够正确打印,但是当我想要在后面使用它的时候,它只包含"na"。不太确定我做错了什么:/
编辑:
这是调用safeAuthCode函数的函数:
function auth(){
chrome.identity.launchWebAuthFlow({
"url": "https://accounts.spotify.com/authorize?client_id="+client_id+
"&redirect_uri="+ encodeURIComponent(redirectUri) +
"&response_type=code"+
"&scope=" + encodeURIComponent(scopes),
"interactive": true
},
function(redirect_url) {
var url = new URL(redirect_url);
var code = url.searchParams.get("code");
safeAuthCode(code);
});
}
sessionStorage
中,这样更有意义。然后在读取/保存时可以使用getItem
和setItem
。 - Icepicklechrome.storage
对象可用于保存数据。 - Icepickle