Javascript scope

I have the code below

it works however console output

instant: true
instant2: false

since the variable is not overwritten in the global scope. How can I access a variable in a 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);
});
+3
source share
2 answers

It becomes overridden, but later. Yours is function(response)not executed until the external function returns.

+6
source

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

0
source

Source: https://habr.com/ru/post/1775146/


All Articles