【发布时间】:2016-04-01 10:25:06
【问题描述】:
我试图通过将所有数据库调用拆分为单独的脚本并使用 ajax 同时运行它们来加快页面加载速度。我做了一些研究,发现 jQuery 有一个 $.when().then() 函数可以解决这个问题。我对 ajax 的基础知识没问题,但延迟/承诺的概念让我有点困惑。我已经尝试了下面的代码,但它只会从第一个 ajax 结果中加载数据。注释掉第一个将加载下一个,但只有一个会显示在页面上。 感谢您对此的任何帮助。谢谢。
$.when( $.post('extensions/ext_adjustments_investigation/general_details.php', {sku: sku},function(data) { result = data; }),
$.post('extensions/ext_adjustments_investigation/location_information.php', {sku: sku},function(data1) { result1 = data1; }),
$.post('extensions/ext_adjustments_investigation/annotations.php', {sku: sku},function(data2) { result2 = data2; }),
$.post('extensions/ext_adjustments_investigation/adjustment_history.php', {sku: sku},function(data3) { result3 = data3; })
).then(
function() {
$("#searching").addClass("hidden");
$("#load").prop("disabled",false);
$("#general").html(result).hide().slideDown()("slow");
$("#location").html(result1).hide().slideDown()("slow");
$("#anno").html(result2).hide().slideDown()("slow");
$("#history").html(result3).hide().slideDown()("slow");
}
);
【问题讨论】:
标签: javascript php jquery ajax jquery-deferred