在Windows 8中,我在一个循环中使用WinJs.xhr来下载一些内容,当它到达“完成回调”后,我想传递一个参数来检索调用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;
}
}
感谢您的帮助
答案 0 :(得分:1)
你需要在一个闭包中捕获它,它带有vanilla for循环/迭代是一种痛苦:
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]);
}