简体   繁体   中英

JavaScript - scope on onload event

I'm new to Javascript and I cannot find what's wrong in my code... Hope you can help me :)

    function addchannel(channel, program) {
            $('#channel-list').append("<li><h2>" + channel.name + " " program.response.name + "</h2></li>");
    };

    var xhr = new XMLHttpRequest({ mozSystem: true });
    xhr.open("GET", "...");
    xhr.responseType="json";
    xhr.send();
    xhr.onload = function(){

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

            xhr2 = new XMLHttpRequest({ mozSystem: true });
            xhr2.open("GET", "..."+channels[i]+"...");
            xhr2.responseType="json";
            xhr2.send();
            xhr2.onload = addchannel(channels[i], xhr2); //HERE IS THE PROBLEM...
        }


    }

I think I have problems with xhr2.onload function (addchannel) that cannot see xhr2... I tried with "this" or something else, but nothing works...

Thank you :)

function addchannel(channel, program) {
        $('#channel-list').append("<li><h2>" + channel.name + " " program.response.name + "</h2></li>");
};

I don't see any issues with scope, but you're missing a plus sign in the addchannel function (" " + program.response) ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM