[英]Wait for all async functions to complete in jQuery
I'm filtering table using jQuery and all is well. 我正在使用jQuery过滤表,一切都很好。 This code works nicely:
这段代码很好地工作:
$("*[id$='EquipmentTypeDropDownList']").change(filterTable);
$("*[id$='StateDropDownList']").change(filterTable);
function filterTable() {
var $equipmentDropDown = $("*[id$='EquipmentTypeDropDownList']");
var $stateDropDown = $("*[id$='StateDropDownList']");
var equipmentFilter = $equipmentDropDown.val();
var stateFilter = $stateDropDown.val();
$("tr.dataRow").each(function () {
var show = true;
var equimpent = $(this).find("td.equipment").text();
var state = $(this).find("td.readyState").text();
if (equipmentFilter != "Any" && equipmentFilter != equimpent) show = false;
if (stateFilter != "Any" && stateFilter != state) show = false;
if (show) {
$(this).fadeIn();
} else {
$(this).fadeOut();
}
});
$("table").promise().done(colorGridRows);
}
function colorGridRows() {
//for table row
$("tr:visible:even").css("background-color", "#DED7D1");
$("tr:visible:odd").css("background-color", "#EEEAE7");
}
colorGridRows
function changes background color of even/odd rows for readability Now, It would be nice if I can replace show/hide calls with fadeIn/fadeOut but I can't because coloring doesn't work (it runs before UI effect completed. If it was just one function parameter - I would just create function for completion and be done with it. But my table has many rows and loop runs through each. How do I wait for ALL to compelete? colorGridRows
函数更改偶数/奇数行的背景颜色以提高可读性现在,如果我可以用showIn / fadeOut替换show / hide调用会很好,但是我不能这样做,因为着色不起作用(它会在UI效果完成之前运行。)它只是一个函数参数-我只是创建要完成的函数并完成该函数,但是我的表有很多行,每行都有循环运行,如何等待ALL完成?
EDITED: Code sample updated showing how I try to use promise()
but it doesn't work. 编辑:代码示例已更新,显示了我如何尝试使用
promise()
但它不起作用。 It fires but I don't get odd/even coloring. 它会点火,但我不会得到奇/偶的颜色。
Use the promise object for animations. 使用promise对象进行动画处理。
$("*[id$='StateDropDownList']").change(function () {
var filtervar = $(this).val();
$('tr td.readyState').each(function () {
if (filtervar == "Any" || $(this).text() == filtervar) {
$(this).parent().fadeIn();
} else {
$(this).parent().fadeOut();
}
}).parent().promise().done(colorGridRows);
//colorGridRows();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.