没有结果我的javascript数组为什么?

时间:2015-10-15 07:12:27

标签: javascript arrays

我推数据数组我可以在数组中看到它,但是没有函数,数组中没有数据为什么?

function keyWordsearch(){

        var musicIDs = [];
        gapi.client.setApiKey('AIzaSyB2O-w22OSJVSKPhAYsK_zxX7FsThM0mhE');
        gapi.client.load('youtube', 'v3', function () {
            idlist = '';


            var q = $('#querySerach').val();
            $("#result").empty();
            var request = gapi.client.youtube.search.list({
                q: q,
                part: 'snippet',
                maxResults: 3,

            });

            request.execute(function (response) {

                for (var i = 0; i < response.items.length; i++) {

                    var musicid2 = response.items[i].id.videoId;

                    musicIDs.push(musicid2);

                   // i push data array i can see it in array but  out of function there is no data in array why ?? ------

                    var pID = '#myPlayerID';
                    var ratio = '4/3';

                }

            });

        });
        alert(musicIDs[1]); // here is musicID[1] return undefined

}

1 个答案:

答案 0 :(得分:1)

Ajax调用是异步,因此你必须在调用api的响应函数中执行 $db['default'] = array( 'dsn' => '', 'hostname' => 'telihatyhighschool.edu.bd', 'username' => 'db_username', 'password' => 'db_password', 'database' => 'db_name', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => TRUE, 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array() , 'save_queries' => TRUE ); 。在这种情况下,在alert

request.execute(function (response) {

更多信息here