0

Hey so I'm creating a basic chrome plugin. I have done no java script until now and have only used java before. For some reason whenever I run this function it always returns false. I have added an alert in the if check and it appears.

function alarmExists() {
    var exists = false;
    
    chrome.alarms.getAll(function(alarms) {
            for(i = 0; i < alarms.length; i++) {
                if(alarms[i].name == "MyAlarm") {
                    alert("True");// This is called
                    exists = true;
                    break;
                }
            }
    });
    return exists;
}

1 Answers1

0

chrome.alarms.getAll is an asynchronous function and takes a callback as an argument. It will execute the callback once it has finished executing which, in your case, happens after you return the value from alarmExists. You should include the logic inside the callback or use a pattern such as Promises.

jmsg1990
  • 165
  • 5