为什么在JS内置数组后调用函数时,我的数组为何显示为空

问题描述

||
// variables to be used throughout
var videos = new Array();

// similar artist/bands
function similarTo(who) {
    $.getJSON(\'http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=\'+who+\'&limit=20&api_key=b25b959554ed76058ac220b7b2e0a026&format=json&callback=?\',function(data) {
        $.each(data,function(i,similars) {
            $.each(similars.artist,function(c,artist) {
                $.getJSON(\'http://gdata.youtube.com/feeds/api/videos?q=\'+artist.name+\'&orderby=relevance&start-index=1&max-results=1&v=2&alt=json-in-script&callback=?\',function(data) {
                    $.each(data.feed.entry,video) {
                        videos.push({ 
                            id: video.id.$t.split(\":\")[3],title: video.title.$t 
                        });
                    });
                });
            });
            initPlaylist();
        });
    });
}

// start the playlist
function initPlaylist() {
    $(\'#ytplayerid\').load(\'includes/ytplayer.php?track=\' + videos[currenttrack].id);
    $(\'#player span\').html(videos[currenttrack].title);
}
当我的代码到达
initPlaylist()
函数时,视频数组似乎为空,我感觉在
$.getJSON()
调用之前它实际上已被触发...这可能吗?如果我在每个“ 3”之后添加console.log(videos),则实际上是在构建数组。     

解决方法

$.each(similars.artist,function(c,artist) {
    // doing ajax stuff here
    $.getJSON(\'url\',function(data) {
        // this will get called later
        $.each(data.feed.entry,function(i,video) {
            videos.push({ 
                id: video.id.$t.split(\":\")[3],title: video.title.$t 
            });
        });
    });
});
// trying to manipulate ajax data now :(
initPlaylist();
您的“ѭ5”是空的,因为您试图在未准备好之前对其进行操作。 您要做的是使用jQuery 1.5+延迟对象
var ajaxs = $.map(similars.artist,function(artist,c) {
    return $.getJSON(\'url\',function(data) {
        $.each(data.feed.entry,title: video.title.$t 
            });
        });
    });
});
// when all the ajaxs finish then call init play list
$.when.apply($,ajaxs).then(initPlaylist);
    ,将
initPlaylist
移动到存在
videos
的位置:
function similarTo(who) {
$.getJSON(\'http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=\'+who+\'&limit=20&api_key=b25b959554ed76058ac220b7b2e0a026&format=json&callback=?\',function(data) {
    $.each(data,similars) {
        $.each(similars.artist,artist) {
            $.getJSON(\'http://gdata.youtube.com/feeds/api/videos?q=\'+artist.name+\'&orderby=relevance&start-index=1&max-results=1&v=2&alt=json-in-script&callback=?\',function(data) {
                var videoes = [];  //create array
                $.each(data.feed.entry,video) {
                    videos.push({ 
                        id: video.id.$t.split(\":\")[3],title: video.title.$t 
                    });
                });
                initPlaylist(); 
            //videos exists,but i think you might need to pass it as a parameter
            });
        });
    });

});
}
虽然,知道ѭ10中的内容可能会有所帮助。它可能会解决您的代码中似乎是范围问题的问题。 还:Ajax是异步的,在代码到达
initPlaylist();
时,它可能还没有完成,因此在完成所有ajax调用后,您需要某种类型的回调来调用
initPlaylist();
。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...