简体   繁体   中英

JavaScript declared global variable can't assign in method

I have some JavaScript code where I have a variable that I've declared outside the function however I can't seem to assign it.

I've looked around and am doing the generally accepted thing of assigning outside the function but still have issues.

Any ideas?

var numberPeopleSignedIn;

function setWaitTime(id, currentTimeToMinus){

var peopleAvailable = [];

  hoodie.store.findAll('barber')
  .done(function(object) {
      object.forEach(function(barber) {
      if (barber.statusInOut == "signedIn") {
        peopleAvailable.push(barber);
      }
      numberPeopleSignedIn = peopleAvailable.length;
    });

  });


console.log(numberPeopleSignedIn);  //This is returning undefined

}

findAll() is returning a delegate. Meaning the console.log is being called before the .done() function when numberPeopleSignedIn is still undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM