1

I'm getting updated value of count in Scope 1 but not 2 and 3. How can i remedy this?

var count = 0;
document.addEventListener("DOMContentLoaded", function(event) {
    chrome.tabs.query({currentWindow: true}, function(tabs) {
        tabs.forEach(function() {
          count++;
        });
        //console.log("Scope 1: " +count);
    });
    //console.log("Scope 2: " +count);
});
//console.log("Scope 3: " +count);
  • Deleted my previous comment because it was not precise. You should be able to access `count` in every scope there because it is a global variable. First of all you have at least 4 scopes here to care about (you did not add a comment to the innermost function object). Second: you should uncomment your log messages to see if the code of that scope gets executed. I am not familiar with google chrome extensions but you should mention in which context you added this code. It might be that the event `DOMContentLoaded` will never be fired because there is no document's DOM being loaded? – Pascal Rosin Nov 23 '17 at 12:26
  • 1
    chrome.* API is asynchronous so the callback runs after the current scope fully finishes. – wOxxOm Nov 23 '17 at 13:03

0 Answers0