无法从函数内部返回值

时间:2015-04-14 12:20:42

标签: jquery variables return-value

此功能不允许我在函数外传递数据。 它返回正确的数据,但每当我尝试将它添加到函数外部的数组或类似数据时,它都不会返回任何内容。 我知道一些冗余的json解析和字符串化,但请忽略它。我试过设置全局变量没有任何运气。

var item.jid = 342323; //example
p = connection.vCard.get(item.jid);   

p.done(function(vCard) {
    var json = JSON.stringify(vCard);
    var json = $.parseJSON(json);

    $(json).each(function(item,val) {
        $.each(val,function(k,v) {
            var json2 = JSON.stringify(v);
            var obj = JSON.parse(json2);
            var what = obj.BINVAL;

            if(what != undefined) {
                var bgimg = what;   
                bgimg = "data:image/jpeg;base64,"+bgimg;
                //console.log(bgimg); //This returns correct data, but only from within this each loop.
                //return bgimg; //this does not return anything
                //arr.push(bgimg);//this does not push anything to array
                //console.log(item.jid); //does not return global var
            }
        });
    });           
});

修改
下面是我试图拉动的功能(改变了romy的回答)以进一步澄清:我正在用化身建立一个接触器。

var bgimg=null;
var arr = new Array();
function getAvatar(vCard) {
            var json = JSON.stringify(vCard);
            var json = $.parseJSON(json);
                $(json).each(function(item,val){
                    $.each(val,function(k,v){
                        var json2 = JSON.stringify(v);
                        var obj = JSON.parse(json2);
                        var what = obj.BINVAL;
                            if(what != undefined){
                                bgimg = what;   
                                bgimg = "data:image/jpeg;base64,"+bgimg;
                                arr.push(bgimg);
                                //console.log(bgimg); //This returns correct data.
                            }

                    }); 
                });
         };

function getRoster(){

connection.roster.requestRoster(function(roster){
   var items = roster.getItems();
   for( var i = 0; i < items.length; i++ ){
        var arr = new Array();
        var item = items[i];
        var thejid = item.jid;
        var thename = item.name;
        arr.push(thejid);
        arr.push(thename);
        p = $.Deferred();
        p = connection.vCard.get(item.jid);   
            p.done(function(vCard) {
                getAvatar(vCard);
            });
       p.resolve();
       console.log(arr);
var theavatar = //This would return whatever the deferred function would throw me. 

        $('.sidebar-nav').append("<li class='contacts'><div id='"+thejid+"' class='avatar' src='"+theavatar+"'></div><a href='#' class='"+thejid+"'>"+thename+"</a></li>");

   }

1 个答案:

答案 0 :(得分:1)

您处于异步/延迟执行模式。

我没有看到任何好处(它也不会有任何好处)像你一样回归:

enter image description here

You can use global variable if you want

var d =  $.Deferred();

var myResult=null;

d.done(function (a){ myResult=a;}) .done(function (){ alert(window["myResult"])})


d.resolve(1); //will alert "1"

但我更喜欢:

var d =  $.Deferred();


function doWhatever(x)
{
  //do wtf you want
  alert(x)
}

var myResult=null;

d.done(function (a){ doWhatever(a)});


d.resolve(1);

http://jsbin.com/potula/1/edit