passing some argument to the callback function of WinJs xhr

假装没事ソ 提交于 2020-01-07 02:29:11

问题


In Windows 8, I am using WinJs.xhr in a loop to download some content, and as it arrives afterward to the "done callback", I would like to pass an argument to retrieve the element who call xhr.

    for (var k = 0 ; k < 9; k++) {
                        var title = dataArray[k].name;
                        if (title != null)
                            url = monUrl+ title;

                        WinJS.xhr({ url: url, responseType: "responseXML" })
   .done(function complete(result) {

    //I would like to retrieve the right title here for example
       var dataArray = new Array();
       var xml = result.responseXML;
 }
}

Thank you for your help


回答1:


You'll need to capture it in a closure, which with vanilla for loops/iteration is a pain:

for (var k = 0 ; k < 9; k++) {
    (function(item) {
        var url;
        var title = item.name;
        if (title != null)
            url = monUrl+ title;

        WinJS.xhr({ url: url, responseType: "responseXML" }).done(function complete(result) {
            // I would like to retrieve the right title here for example
            var dataArray = new Array();
            var xml = result.responseXML;
            /* use your title property here */
        });
    })(dataArray[k]);
}


来源:https://stackoverflow.com/questions/11888049/passing-some-argument-to-the-callback-function-of-winjs-xhr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!