如何确保此javascript的顺序?

如何确保此javascript的顺序?,javascript,asynchronous,Javascript,Asynchronous,为了开始我的页面,我与facebook进行了一次检查,以获取用户的登录状态。然后,我在window对象中创建一个名称空间,以便在其他javascript文件中使用。在加载任何其他javascript之前,我需要创建这个名称空间,否则当我尝试访问它时,它将是未定义的。有时它先完成,有时不完成。我如何确保每次都正确发生 . . . FB.getLoginStatus(function(response) { // store data on the user in

为了开始我的页面,我与facebook进行了一次检查,以获取用户的登录状态。然后,我在window对象中创建一个名称空间,以便在其他javascript文件中使用。在加载任何其他javascript之前,我需要创建这个名称空间,否则当我尝试访问它时,它将是未定义的。有时它先完成,有时不完成。我如何确保每次都正确发生

.
.
.
        FB.getLoginStatus(function(response) {
          // store data on the user inside of a namespace 
          // inside of the window object so it can be accessed
          // from anywhere. Use this like '$_SESSION'
          console.log('doing it');
          window.easyUserData = {
            fbRespose: response,
            site: <?php echo '"'.$site.'"'; ?>
          };
        })
      };

      (function(d) {
        var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
        if (d.getElementById(id)) {return;}
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        ref.parentNode.insertBefore(js, ref);
      }(document))
    </script>
    <!-- Load the script "js/main.js" as our entry point for require -->
    <script data-main="js/main" src="lib/Require/require.js"></script>
。
.
.
FB.getLoginStatus(函数(响应){
//将用户上的数据存储在命名空间内
//窗口对象的内部,以便可以访问它
//从任何地方。使用类似于“$\u会话”的方法
log('doing it');
window.easyUserData={
fbRespose:响应,
地点:
};
})
};
(职能(d){
var js,id='facebook jssdk',ref=d.getElementsByTagName('script')[0];
if(d.getElementById(id)){return;}
js=d.createElement('script');js.id=id;js.async=true;
js.src=“//connect.facebook.net/en_US/all.js”;
ref.parentNode.insertBefore(js,ref);
}(文件)
具体而言,我需要:

<script data-main="js/main" src="lib/Require/require.js"></script>


在facebook函数完成之前不会发生

将依赖于名称空间的代码移动到facebook回调中。如果是
,则动态创建
脚本
元素:

  // ...

  FB.getLoginStatus(function(response) {
    // store data on the user inside of a namespace 
    // inside of the window object so it can be accessed
    // from anywhere. Use this like '$_SESSION'
    console.log('doing it');
    window.easyUserData = {
      fbRespose: response,
      site: <?php echo '"'.$site.'"'; ?>
    };

    // Create the script element when ready
    var script = document.createElement('script');
    script.setAttribute("data-main", "js/main");
    script.src = "lib/Require/require.js";
    document.getElementsByTagName('script')[0].parentNode.appendChild(script);
    // Or instead of `document.getElementsByTagName('script')[0].parentNode`
    // you can use `document.documentElement` or `document.getElementsByTagName('head')[0]
    // or a few others
  })
};
/。。。
FB.getLoginStatus(函数(响应){
//将用户上的数据存储在命名空间内
//窗口对象的内部,以便可以访问它
//从任何地方。使用类似于“$\u会话”的方法
log('doing it');
window.easyUserData={
fbRespose:响应,
地点:
};
//准备好后创建脚本元素
var script=document.createElement('script');
script.setAttribute(“数据主”、“js/main”);
script.src=“lib/Require/Require.js”;
document.getElementsByTagName('script')[0].parentNode.appendChild(script);
//或者代替'document.getElementsByTagName('script')[0].parentNode`
//您可以使用'document.documentElement'或'document.getElementsByTagName('head')[0]
//或者其他一些
})
};
您可以按照TJ的建议将其全部移动到一个地方。或者,您可以在尝试运行自己的代码之前检查变量是否已创建:

// First this:
FB.getLoginStatus(function(response) {
    window.easyUserData = {
        fbRespose: response,
        site: <?php echo '"'.$site.'"'; ?>
    };
});

// Later in the code:
(function () {
    var isFBLoaded = function () {
        // If FB has loaded - run our code
        if (window.easyUserData) {
            myOtherFBStuff();
        }
        // If not - check again in 500 ms
        else {
            setTimeout(isFBLoaded, 500);
        }
    };

    var myOtherFBStuff = function () {
        // do stuff here
    };

    isFBLoaded();
})();
//首先:
FB.getLoginStatus(函数(响应){
window.easyUserData={
fbRespose:响应,
地点:
};
});
//代码后面部分:
(功能(){
var isFBLoaded=函数(){
//如果FB已加载-运行我们的代码
if(window.easyUserData){
myOtherFBStuff();
}
//如果没有-请在500毫秒后再次检查
否则{
setTimeout(isFBLoaded,500);
}
};
var myOtherFBStuff=函数(){
//在这里做事
};
isFBLoaded();
})();

很抱歉,我需要在fb之后出现“”callback@TroyCosentino:这是我的第二个猜测,刚刚补充道::-)