开发者

javascript variable scope

I have the below code,

it is running however the output in the consol开发者_如何学编程e is

instant: true

instant2: false

as the variable is not being overwritten in the global scope. How can I access the variable in the global scope?

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);
});


It is getting overridden, but later. Your function(response) isn't getting executed until after the outer function returns.


window.instant should get you the value of your global variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜