我有下面的代码,
它正在运行,但是控制台中的输出是
instant: true
instant2: false
因为变量没有在全局范围内被覆盖。 如何在全局范围内访问变量?
var instant = false;
$('document').ready(function(){
chrome.extension.sendRequest({
action: "getStorage",
key: "instant"
}, function(response) {
instant = true;
console.log('instant: ', instant);
});
console.log('instant2: ', instant);
});
最佳答案
它正在被覆盖,但稍后。您的 function(response)
直到外部函数返回后才会执行。
关于javascript变量作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4192597/