Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何判断JavaScript异步函数何时结束?_Javascript_Jquery_Facebook - Fatal编程技术网

如何判断JavaScript异步函数何时结束?

如何判断JavaScript异步函数何时结束?,javascript,jquery,facebook,Javascript,Jquery,Facebook,我试图通过一些标志来确定这个异步函数何时结束 这就是我调用函数的方式: // Calling the function in a loop for (var i=0,l=myIds.length; i<l; ++i) { install(pageId, myIds[i], apps_num[''+myIds[i]], <?php echo $this->comid ?>, '<?php echo $this->site_url; ?>'); }

我试图通过一些标志来确定这个异步函数何时结束

这就是我调用函数的方式:

// Calling the function in a loop
for (var i=0,l=myIds.length; i<l; ++i) {
    install(pageId, myIds[i], apps_num[''+myIds[i]], <?php echo $this->comid ?>, '<?php echo $this->site_url; ?>');
}
install: function(pageId, appId, app_num, com_id, siteurl) {
    FB.getLoginStatus(function(response) {
        // Checking if connected to Facebook
        if (response.status === 'connected') {
            var uid = response.authResponse.userID;
            console.log(response.authResponse);
            var userAccessToken = response.authResponse.accessToken;

            // Get page access token
            FB.api('/'+pageId+'?fields=access_token='+userAccessToken, function(response) {
                var pageAccessToken = response.access_token;

                // Get information if user got this application
                FB.api('/'+pageId+'/tabs/'+appId+'?access_token='+pageAccessToken,
                    function(data) {
                        if (data.data.length < 1) {
                            console.log("Not installed, Installing...");

                            // Install the application
                            var params = {};
                            params['app_id'] = appId;
                            FB.api('/'+pageId+'/tabs?access_token='+pageAccessToken, 'post', params, function(response) {
                                if (!response || response.error) {
                                    console.log("Error Installing!");
                                }
                                else {
                                    console.log("Installed :)");
                                }
                            });
                        }
                        else {
                            console.log("Already installed.");
                        }
                    });
                });
            }
            else
                if (response.status === 'not_authorized') {
                    console.log("the user is logged in to Facebook, but not connected to the app.");
                }
                else {
                    console.log("the user isn't even logged in to Facebook.");
                }
            });
        }
install: function(pageId, appId, app_num, com_id, siteurl, callback)
//                                        here ------------^
for (var i=0,l=myIds.length; i<l; ++i) {
    install(pageId, myIds[i], apps_num[''+myIds[i]], <?php echo $this->comid ?>, '<?php echo $this->site_url; ?>', function(){
        //do whatever you want;
    });
}
//在循环中调用函数
对于(var i=0,l=myIds.length;i,”);
}
这是我的职责:

// Calling the function in a loop
for (var i=0,l=myIds.length; i<l; ++i) {
    install(pageId, myIds[i], apps_num[''+myIds[i]], <?php echo $this->comid ?>, '<?php echo $this->site_url; ?>');
}
install: function(pageId, appId, app_num, com_id, siteurl) {
    FB.getLoginStatus(function(response) {
        // Checking if connected to Facebook
        if (response.status === 'connected') {
            var uid = response.authResponse.userID;
            console.log(response.authResponse);
            var userAccessToken = response.authResponse.accessToken;

            // Get page access token
            FB.api('/'+pageId+'?fields=access_token='+userAccessToken, function(response) {
                var pageAccessToken = response.access_token;

                // Get information if user got this application
                FB.api('/'+pageId+'/tabs/'+appId+'?access_token='+pageAccessToken,
                    function(data) {
                        if (data.data.length < 1) {
                            console.log("Not installed, Installing...");

                            // Install the application
                            var params = {};
                            params['app_id'] = appId;
                            FB.api('/'+pageId+'/tabs?access_token='+pageAccessToken, 'post', params, function(response) {
                                if (!response || response.error) {
                                    console.log("Error Installing!");
                                }
                                else {
                                    console.log("Installed :)");
                                }
                            });
                        }
                        else {
                            console.log("Already installed.");
                        }
                    });
                });
            }
            else
                if (response.status === 'not_authorized') {
                    console.log("the user is logged in to Facebook, but not connected to the app.");
                }
                else {
                    console.log("the user isn't even logged in to Facebook.");
                }
            });
        }
install: function(pageId, appId, app_num, com_id, siteurl, callback)
//                                        here ------------^
for (var i=0,l=myIds.length; i<l; ++i) {
    install(pageId, myIds[i], apps_num[''+myIds[i]], <?php echo $this->comid ?>, '<?php echo $this->site_url; ?>', function(){
        //do whatever you want;
    });
}
install:function(pageId、appId、appnum、com\u id、siteurl){
FB.getLoginStatus(函数(响应){
//检查是否连接到Facebook
如果(response.status===“已连接”){
var uid=response.authResponse.userID;
log(response.authResponse);
var userAccessToken=response.authResponse.accessToken;
//获取页面访问令牌
api('/'+pageId+'?fields=access\u token='+userAccessToken,函数(响应){
var pageAccessToken=response.access\u令牌;
//如果用户获得此应用程序,请获取信息
FB.api('/'+pageId+'/tabs/'+appId+'?访问令牌='+pageAccessToken,
功能(数据){
如果(data.data.length<1){
日志(“未安装,正在安装…”);
//安装应用程序
var params={};
参数['app_id']=appId;
api('/'+pageId+'/tabs?access_token='+pageAccessToken,'post',参数,函数(响应){
如果(!response | | response.error){
log(“安装错误!”);
}
否则{
console.log(“已安装:)”;
}
});
}
否则{
log(“已安装”);
}
});
});
}
其他的
如果(response.status==‘未授权’){
log(“用户已登录到Facebook,但未连接到应用程序。”);
}
否则{
log(“用户甚至没有登录到Facebook。”);
}
});
}

我如何解决这个问题?我尝试使用静态变量,但无法在异步函数中调用它们。

通常的做法是让需要知道异步调用结果的任何代码将函数引用传递到调用中,然后函数在调用完成时调用该调用(“回调”)

因此,在本例中,您需要向
install
函数添加一个回调参数:

// Calling the function in a loop
for (var i=0,l=myIds.length; i<l; ++i) {
    install(pageId, myIds[i], apps_num[''+myIds[i]], <?php echo $this->comid ?>, '<?php echo $this->site_url; ?>');
}
install: function(pageId, appId, app_num, com_id, siteurl) {
    FB.getLoginStatus(function(response) {
        // Checking if connected to Facebook
        if (response.status === 'connected') {
            var uid = response.authResponse.userID;
            console.log(response.authResponse);
            var userAccessToken = response.authResponse.accessToken;

            // Get page access token
            FB.api('/'+pageId+'?fields=access_token='+userAccessToken, function(response) {
                var pageAccessToken = response.access_token;

                // Get information if user got this application
                FB.api('/'+pageId+'/tabs/'+appId+'?access_token='+pageAccessToken,
                    function(data) {
                        if (data.data.length < 1) {
                            console.log("Not installed, Installing...");

                            // Install the application
                            var params = {};
                            params['app_id'] = appId;
                            FB.api('/'+pageId+'/tabs?access_token='+pageAccessToken, 'post', params, function(response) {
                                if (!response || response.error) {
                                    console.log("Error Installing!");
                                }
                                else {
                                    console.log("Installed :)");
                                }
                            });
                        }
                        else {
                            console.log("Already installed.");
                        }
                    });
                });
            }
            else
                if (response.status === 'not_authorized') {
                    console.log("the user is logged in to Facebook, but not connected to the app.");
                }
                else {
                    console.log("the user isn't even logged in to Facebook.");
                }
            });
        }
install: function(pageId, appId, app_num, com_id, siteurl, callback)
//                                        here ------------^
for (var i=0,l=myIds.length; i<l; ++i) {
    install(pageId, myIds[i], apps_num[''+myIds[i]], <?php echo $this->comid ?>, '<?php echo $this->site_url; ?>', function(){
        //do whatever you want;
    });
}
…然后在适当的时候从传递到
FB.getLoginStatus
和/或
FB.api
的回调调用它,例如:

install: function(pageId, appId, app_num, com_id, siteurl, callback) {
        FB.getLoginStatus(function(response) {
                       // checking if connected to facebook
              if (response.status === 'connected') {
                var uid = response.authResponse.userID;
                console.log(response.authResponse);
                var userAccessToken = response.authResponse.accessToken;

                // get page access token
                FB.api('/'+pageId+'?fields=access_token='+userAccessToken, function(response) {
                    var pageAccessToken = response.access_token;

                    // get information if user got this app
                    FB.api('/'+pageId+'/tabs/'+appId+'?access_token='+pageAccessToken,
                       function(data) {
                         if (data.data.length < 1) {
                             console.log("Not installed, Installing...");

                            // install the app
                             var params = {};
                             params['app_id'] = appId;
                             FB.api('/'+pageId+'/tabs?access_token='+pageAccessToken, 'post', params, function(response) {
                                 if (!response || response.error) {
                                    callback(false, "Error installing");
                                    console.log("Error Installing!");
                                 } else {
                                    callback(true, "Installed");
                                    console.log("Installed :)");

                                 }
                                });
                         }
                         else {
                             callback(false, "Already installed.");
                             console.log("Already installed.");
                         }
                     });
                });
              } else if (response.status === 'not_authorized') {
                callback(false, "Logged in but not connected.");
                console.log("the user is logged in to Facebook, but not connected to the app.");
              } else {
                callback(false, "Not logged in.");
                console.log("the user isn't even logged in to Facebook.");
              }
             });
      }
install:function(pageId、appId、app\u num、com\u id、siteurl、回调){
FB.getLoginStatus(函数(响应){
//检查是否连接到facebook
如果(response.status===“已连接”){
var uid=response.authResponse.userID;
log(response.authResponse);
var userAccessToken=response.authResponse.accessToken;
//获取页面访问令牌
api('/'+pageId+'?fields=access\u token='+userAccessToken,函数(响应){
var pageAccessToken=response.access\u令牌;
//如果用户获得此应用,请获取信息
FB.api('/'+pageId+'/tabs/'+appId+'?访问令牌='+pageAccessToken,
功能(数据){
如果(data.data.length<1){
日志(“未安装,正在安装…”);
//安装应用程序
var params={};
参数['app_id']=appId;
api('/'+pageId+'/tabs?access_token='+pageAccessToken,'post',参数,函数(响应){
如果(!response | | response.error){
回调(false,“安装错误”);
log(“安装错误!”);
}否则{
回调(true,“已安装”);
console.log(“已安装:)”;
}
});
}
否则{
回调(false,“已安装”);
log(“已安装”);
}
});
});
}else if(response.status===“未授权”){
回调(false,“已登录但未连接”);
log(“用户已登录到Facebook,但未连接到应用程序。”);
}否则{
回调(false,“未登录”);
log(“用户甚至没有登录到Facebook。”);
}
});
}

在这里,我给了回调函数两个参数:一个表示是否执行了安装的布尔值和一条状态消息。

首先,向函数添加一个额外的参数,该参数将接收一个函数对象

安装:函数(pageId、appId、appnum、com\u id、siteurl、callbackFunction){

然后,在函数安装之后

console.log("Installed :)");
你加上

callbackFunction();
这样,当您调用安装函数时:

// Calling the function in a loop
for (var i=0,l=myIds.length; i<l; ++i) {
    install(pageId, myIds[i], apps_num[''+myIds[i]], <?php echo $this->comid ?>, '<?php echo $this->site_url; ?>');
}
install: function(pageId, appId, app_num, com_id, siteurl) {
    FB.getLoginStatus(function(response) {
        // Checking if connected to Facebook
        if (response.status === 'connected') {
            var uid = response.authResponse.userID;
            console.log(response.authResponse);
            var userAccessToken = response.authResponse.accessToken;

            // Get page access token
            FB.api('/'+pageId+'?fields=access_token='+userAccessToken, function(response) {
                var pageAccessToken = response.access_token;

                // Get information if user got this application
                FB.api('/'+pageId+'/tabs/'+appId+'?access_token='+pageAccessToken,
                    function(data) {
                        if (data.data.length < 1) {
                            console.log("Not installed, Installing...");

                            // Install the application
                            var params = {};
                            params['app_id'] = appId;
                            FB.api('/'+pageId+'/tabs?access_token='+pageAccessToken, 'post', params, function(response) {
                                if (!response || response.error) {
                                    console.log("Error Installing!");
                                }
                                else {
                                    console.log("Installed :)");
                                }
                            });
                        }
                        else {
                            console.log("Already installed.");
                        }
                    });
                });
            }
            else
                if (response.status === 'not_authorized') {
                    console.log("the user is logged in to Facebook, but not connected to the app.");
                }
                else {
                    console.log("the user isn't even logged in to Facebook.");
                }
            });
        }
install: function(pageId, appId, app_num, com_id, siteurl, callback)
//                                        here ------------^
for (var i=0,l=myIds.length; i<l; ++i) {
    install(pageId, myIds[i], apps_num[''+myIds[i]], <?php echo $this->comid ?>, '<?php echo $this->site_url; ?>', function(){
        //do whatever you want;
    });
}
for(var i=0,l=myIds.length;i',function(){
//做你想做的事;
});
}

jQuery通过让用户确定函数完成时应该如何反应来实现这一点。这意味着,将函数作为参数。这叫做callb