问题
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