开发者

javascript variable scope

开发者 https://www.devze.com 2023-01-25 02:19 出处:网络
I have the below code, it is running however the output in the consol开发者_如何学编程e is instant: true

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

精彩评论

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