有没有一种方法可以在异步加载后运行函数?

时间:2019-06-11 07:41:14

标签: javascript jquery asynchronous

在提供的代码中,我正在使用getJSON将值推入数组并将其设置为全局数组,以便可以在异步之后使用它。我希望我拥有的函数(makeGraph())能够在页面加载时与包含的异步数据一起运行,并且想知道是否有可能这样做。

旁注以保持清晰
当window.onload调用makeGraph()时,将记录未定义。 通过按钮调用makeGraph()时,将记录文件路径。

<button onclick="makeGraph()">Submit</button>
    var songs = {};
    var unames = {};

    $.getJSON('audio-player/musicque.json', function(data) {

        var temparr = new Array();
        $.each(data, function(key, value) {
            temparr.push(value['filepath']);
        });
            songs = temparr;

    });

    window.onload = makeGraph;

    function makeGraph() {

        console.log(songs[1]);

    }

0 个答案:

没有答案
相关问题