简体   繁体   English

JavaScript声明的全局变量无法在方法中分配

[英]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. 我有一些JavaScript代码,其中有一个已在函数外部声明的变量,但是似乎无法分配它。

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. findAll()返回一个委托。 Meaning the console.log is being called before the .done() function when numberPeopleSignedIn is still undefined. 这意味着当numberPeopleSignedIn仍未定义时,在.done()函数之前调用console.log。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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