Javascript 在循环中使用承诺模式
我需要循环通过div并使用promise模式加载它们,但显然只显示上次调用的数据 这是我的密码Javascript 在循环中使用承诺模式,javascript,jquery,Javascript,Jquery,我需要循环通过div并使用promise模式加载它们,但显然只显示上次调用的数据 这是我的密码 $('div[class=ceTable]').each(function () { var position = $(this).position(); gridID = $(this).attr('id') tableID = $(this).attr("data-tableid") docId = $(this).attr("data-docid") he
$('div[class=ceTable]').each(function () {
var position = $(this).position();
gridID = $(this).attr('id')
tableID = $(this).attr("data-tableid")
docId = $(this).attr("data-docid")
headerFound = $(this).data("headerFound")
headerArray = $(this).data("headerArray")
columnCount = $(this).data("columnCount")
$.ajax({
type: "GET",
dataType: "json",
url: "ajaxGetTableData",
data: {
'docID': docId,
'tableID': tableID
},
beforeSend: function () {
$('#' + gridID).block({
css: {
border: 'none',
padding: '15px',
backgroundColor: '#36a9e1',
'-webkit-border-radius': '10px',
'-moz-border-radius': '10px',
opacity: 5,
color: '#fff'
},
message: 'Loading Grid'
});
}
}).done(function (data) {
console.log(data, "ajaxGetTableData")
ceFeature.generateGridFromJSONObject({
tabledata: data,
columnCount: columnCount,
gridID: gridID,
headerArray: headerArray,
headerFound: headerFound
})
$('#' + gridID).unblock();
})
使用闭包:
$('div[class=ceTable]').each(function () {
var position = $(this).position();
gridID = $(this).attr('id')
tableID = $(this).attr("data-tableid")
docId = $(this).attr("data-docid")
headerFound = $(this).data("headerFound")
headerArray = $(this).data("headerArray")
columnCount = $(this).data("columnCount")
(function (columnCount, gridID, headerArray, headerFound) {
$.ajax().done();
}(columnCount, gridID, headerArray, headerFound));
});
使用闭包:
$('div[class=ceTable]').each(function () {
var position = $(this).position();
gridID = $(this).attr('id')
tableID = $(this).attr("data-tableid")
docId = $(this).attr("data-docid")
headerFound = $(this).data("headerFound")
headerArray = $(this).data("headerArray")
columnCount = $(this).data("columnCount")
(function (columnCount, gridID, headerArray, headerFound) {
$.ajax().done();
}(columnCount, gridID, headerArray, headerFound));
});
您的变量是隐式全局的(因为您忘记了),所以每次迭代都将覆盖以前的值。异步回调将只访问最后一个,然后是典型的 要解决此问题,请将变量设置为函数的局部变量(每个回调),以便成功回调在其作用域中包含相应的变量:
$('div[class=ceTable]').each(function () {
var position = $(this).position(),
gridID = $(this).attr('id'),
tableID = $(this).attr("data-tableid"),
docId = $(this).attr("data-docid"),
headerFound = $(this).data("headerFound"),
headerArray = $(this).data("headerArray"),
columnCount = $(this).data("columnCount");
…
您的变量是隐式全局的(因为您忘记了),所以每次迭代都将覆盖以前的值。异步回调将只访问最后一个,然后是典型的 要解决此问题,请将变量设置为函数的局部变量(每个回调),以便成功回调在其作用域中包含相应的变量:
$('div[class=ceTable]').each(function () {
var position = $(this).position(),
gridID = $(this).attr('id'),
tableID = $(this).attr("data-tableid"),
docId = $(this).attr("data-docid"),
headerFound = $(this).data("headerFound"),
headerArray = $(this).data("headerArray"),
columnCount = $(this).data("columnCount");
…
必须在每个()loopOuch内使用闭包必须在每个()loopOuch内使用闭包。真的需要这么复杂吗?@Bergi没关系,我现在明白了;)哎哟真的需要这么复杂吗?@Bergi没关系,我现在明白了;)哇,我没看见,甚至没想过!谢谢!谢谢你的建议。我只是用延迟调用做了一个函数,它成功了。阅读有关在MDN站点上的函数中创建闭包的内容,这不是推荐的方法。闭包是推荐的(并且是唯一可能的)方法。MDN上的警告只是说明您可以滥用它们。在这种情况下,您已经通过使用
$()得到了一个闭包。每个-您不应该像a.Wolff的回答中那样使用两个闭包。哇,我没有看到它,甚至没有想到它!谢谢!谢谢你的建议。我只是用延迟调用做了一个函数,它成功了。阅读有关在MDN站点上的函数中创建闭包的内容,这不是推荐的方法。闭包是推荐的(并且是唯一可能的)方法。MDN上的警告只是说明您可以滥用它们。在这种情况下,通过使用$(),您已经有了一个闭包。每个-您不应该像a.Wolff的回答中那样使用两个闭包。