无法使对象失去功能 [英] Unable to get Object out of Function

查看:131
本文介绍了无法使对象失去功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个很棒的项目,通常我使用AS3,但现在我正在使用Javascript。



我有以下信息:下面我试图访问检测线,基本上我需要的是detect(response.username)。我试过很多东西,有人可以帮忙吗?

 < div id =fb-root>< / DIV> 
< script type =text / javascript>


$(document).ready(function(){


var appId =121070974711874;
//如果记录作为Facebook画布应用程序使用此URL
var redirectUrl =http://apps.facebook.com/bggressive;
//如果作为网站登录,请确保添加您的应用程序的应用程序域列表的主机
var redirectUrl = window.location.href;




//如果用户没有授予应用程序授权继续,
//告诉他们停止代码执行
if(0< = window.location.href.indexOf(error_reason))
{
$(document.body).append(< p>授权拒绝!< / p>);
return;
}


//当Facebook SDK脚本完成加载init
// SDK然后获取用户的登录状态时,状态为
//在处理程序中报告
window.fbAsyncInit = function(){


FB.init({
appId:appId,
status:true,
cookie:true,
oauth:true
});


FB.getLoginStatus(onCheckLoginStatus);
};



(function(d)
{
var js,id ='facebook-jssdk'; if(d.getElementById(id) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src =//connect.facebook.net/en_US/ all.js;
d.getElementsByTagName('head')[0] .appendChild(js);
}(document));



函数onCheckLoginStatus(响应)
{
if(response.status!=connected)
{
top.location.href =https://www.facebook.com/dialog/oauth?client_id=+ appId +& redirect_uri =+ encodeURIComponent(redirectUrl)+& scope = user_photos,friends_photos;
}
else
{$
//启动应用程序(这只是演示代码)!
$(document.body).append;

FB.api('/ me?fields = username',function(response){
function detect(URL){
var image = new Image();
image.src = URL;
image.onload = function(){
var result ='result'; //一个示例结果

}

document.body.appendChild(image)

}
detect(https://graph.facebook.com/+ response.username +/ picture?width = 200& ; height = 200);








});



FB.api('/ me / friends?fields = id,first_name',function(response){
var randomFriend = Math.floor(getRandom (0,response.data.length));
gFriendID = response.data [randomFriend] .id;
var randomFriend = Math.floor(getRandom(0,response.data.length));
gFriendID2 = response.data [randomFriend] .id;



函数friend1(URL){
var image = new Image();
image.src = URL;
image.onload = function(){
var result ='result'; //一个示例结果

}

document.body.appendChild(image)

}
friend1(https://graph.facebook.com/+ gFriendID +/ picture?width = 200& height = 200);


函数friend2(URL){
var image = new Image();
image.src = URL;
image.onload = function(){
var result ='result'; //一个示例结果

}

document.body.appendChild(image)

}
friend2(https:// graph.facebook.com/+ gFriendID2 +/ picture?width = 200& height = 200);


});





}
}
});
< / script>


解决方案

如果我理解正确,的当前登录用户,然后检测,当图像加载。您正在尝试修复这部分代码:

  FB.api('/ me?fields = username',function响应){
功能检测(URL){
var image = new Image();
image.src = URL;
image.onload = function(){
var result ='result'; //一个示例结果

}

document.body.appendChild(image)

}
detect(https://graph.facebook.com/+ response.username +/ picture?width = 200& height = 200);
});

这里有很多事情。首先,您需要知道要加载的图像的所有信息将从 / me / 调用中提供给您。你必须传递给函数的唯一的事情就是回调(当加载图像时该怎么做),以及可能的宽度和高度。



其次,如果您设置了图像的 src 属性,则图像将被加载。如果您在 src 属性之后设置 onload 处理程序,并从缓存中提取图像,则图像已经已经完成加载, onload 事件已经被触发,并且处理程序将不被调用。这在这篇文章中被更好地解释了



所以,你可以这样做:

  function detect(username){
var result ='result'; //一个示例结果
}

函数displayPicture(width,height,callback){
FB.api('/ me?fields = username',function(response){
var image = new Image();
image.onload = function(){
callback(response.username);
};
image.src = https://graph.facebook.com/+ response.username +/ picture?width = 200& height = 200;
document.body.appendChild(image);
});
}

displayPicture(200,200,detect);
//或
displayPicture(200,200,function(username){
var result ='result'; //一个示例结果
));

嗯,祝你好运!我希望这实际上是你想要做的。


I am working on an awesome project, normally i use AS3 but now I am using Javascript.

I have the following information: below I am trying to access the detect line, basically what i need is detect(response.username). I have tried a lot of things, can anyone help?

<div id="fb-root"></div>
<script type="text/javascript">


$(document).ready(function(){


    var appId = "121070974711874";
    // If logging in as a Facebook canvas application use this URL.
    var redirectUrl = "http://apps.facebook.com/bggressive";
    // If logging in as a website do this. Be sure to add the host to your application's App Domain list. 
    var redirectUrl = window.location.href;




    // If the user did not grant the app authorization go ahead and
    // tell them that. Stop code execution.
    if (0 <= window.location.href.indexOf ("error_reason"))
    {
        $(document.body).append ("<p>Authorization denied!</p>");
        return;
    }


    // When the Facebook SDK script has finished loading init the
    // SDK and then get the login status of the user. The status is
    // reported in the handler.
    window.fbAsyncInit = function(){


        FB.init({
            appId : appId,
            status : true,
            cookie : true,
            oauth : true
        });


        FB.getLoginStatus (onCheckLoginStatus);
    };



    (function(d)
    {
        var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        d.getElementsByTagName('head')[0].appendChild(js);
    }(document));



    function onCheckLoginStatus (response)
    {
        if (response.status != "connected")
        {
            top.location.href = "https://www.facebook.com/dialog/oauth?client_id=" + appId + "&redirect_uri=" + encodeURIComponent (redirectUrl) + "&scope=user_photos,friends_photos";
        }
        else
        {$
            // Start the application (this is just demo code)!
            $(document.body).append ;

            FB.api('/me?fields=username', function(response) {
                function detect(URL) {
                    var image = new Image();
                    image.src = URL;
                    image.onload = function() {
                        var result = 'result'; // An example result

                    }

                    document.body.appendChild(image)

                }
                detect("https://graph.facebook.com/" + response.username + "/picture?width=200&height=200");








            });



            FB.api('/me/friends?fields=id,first_name', function(response) {
                var randomFriend = Math.floor(getRandom(0, response.data.length));
                gFriendID = response.data[randomFriend].id;
                var randomFriend = Math.floor(getRandom(0, response.data.length));
                gFriendID2 = response.data[randomFriend].id;



                function friend1(URL) {
                    var image = new Image();
                    image.src = URL;
                    image.onload = function() {
                        var result = 'result'; // An example result

                    }

                    document.body.appendChild(image)

                }
                friend1("https://graph.facebook.com/" + gFriendID + "/picture?width=200&height=200");


                function friend2(URL) {
                    var image = new Image();
                    image.src = URL;
                    image.onload = function() {
                        var result = 'result'; // An example result

                    }

                    document.body.appendChild(image)

                }
                friend2("https://graph.facebook.com/" + gFriendID2 + "/picture?width=200&height=200");


            });





        }
    }
});
</script>

解决方案

If I understand correctly, you want to display the image of the currently logged in user, and then detect when the image has loaded. You are trying to fix this part of the code:

FB.api('/me?fields=username', function(response) {
    function detect(URL) {
        var image = new Image();
        image.src = URL;
        image.onload = function() {
            var result = 'result'; // An example result

        }

        document.body.appendChild(image)

    }
    detect("https://graph.facebook.com/" + response.username + "/picture?width=200&height=200");
});

There's a number of things going on here. First, all the information you need to know which image to load will be supplied to you from the /me/ call. The only thing you have to pass to the function is a callback (what to do when the image loads), and possibly the width and height.

Second, if you set the src property of your image, the image will be loaded. If you set the onload handler after the src property, and the image is fetched from the cache, the image will already have finished loading, the onload event will already have fired, and the handler won't be called. This is explained a lot better in this post.

So, you could do it like this:

function detect(username) {
    var result = 'result'; // An example result
}

function displayPicture(width, height, callback) {
    FB.api('/me?fields=username', function(response) { 
        var image = new Image();
        image.onload = function() {
            callback(response.username);
        };
        image.src = "https://graph.facebook.com/" + response.username + "/picture?width=200&height=200";
        document.body.appendChild(image);
    });
}

displayPicture(200, 200, detect);
// or
displayPicture(200, 200, function(username) {
    var result = 'result'; // An example result
));

Well, good luck! I hope this was, in fact, what you were trying to do.

这篇关于无法使对象失去功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆