google chrome extension - Global variable is defined within function and also expires in function javascript -
this question has answer here:
i creating chrome extension , surprise content script acting odd. have defined global variable keyword when goes through function gives it's value, value expires when function ends. i've spent hours troubleshooting , can't seem find problem.
var keyword; chrome.extension.sendmessage({localstorage: "yo"}, function(response) { var localstorage = response.localstorage; keyword = localstorage["keyword"]; console.log(keyword); }); console.log(keyword); the log within function returns desired value, log after function returns undefined, though far can tell, should return value given within function. did not declare within function, why it's value expire after function?
the value "keyword" assigned in kind of callback function seems in example. if run code, callback function not executed immediately, no value assigned "keyword". executed later, when chrome extensions fires callback function, passed "sendmessage".
but console.log executed , "keyword" initialized - undefined.
Comments
Post a Comment