【问题标题】:How to synchronise '.append()' and ajax query within 'for' loop如何在'for'循环中同步'.append()'和ajax查询
【发布时间】:2017-12-28 18:24:12
【问题描述】:

大家好!似乎我有异步操作问题,我不知道如何处理。

我希望显示此代码

第 1 类,

第 1 类物品

第 2 类,

第 2 类物品

第 3 类,

第 3 类物品

我得到的是:

第 1 类,

第 2 类,

第 3 类,

第 1 类物品

第 2 类物品

第 3 类物品

代码如下:

function showSubheadersMenu (items) {
    for (j = 0; j < items.length; j++) {
      $("#gallery").append($("<div id=" + items[j].id + " class=\"gallery-item\"><div class=\"item-img\" style=\"background-image:url(" + items[j].better_featured_image.source_url + ")\"></div></div>"));
    }
  }

 function showSingleCategory (subheader) {
    $("#gallery").append($("<h3 class=\"subcat\">" + subheader.name + "</h3>"));

    $.ajax({
      url: "http://localhost/mysite/wp-json/wp/v2/posts?categories=" + subheader.id + "",
      dataType: "json",
      contentType: "GET",

      success: showSubcategoryItems, // Show items in subcategory

      error: handleAjaxError
   });
 }

if (subheaders.length > 0) { //If there are subcategories
   for (i = 0; i < subheaders.length; i++) {
     showSingleCategory(subheaders[i]);
   }
}

【问题讨论】:

  • 尝试将$("#gallery").append($("&lt;h3&gt;(...)&lt;/h3&gt;")); 包裹在成功函数中
  • 你能把showSubcategoryItems加到这里吗?
  • @agrm 有效!谢谢!

标签: jquery ajax wordpress asynchronous


【解决方案1】:

由于 ajax 的异步性,所有的“成功”调用都保证在所有 &lt;h3&gt; 元素被附加后发生。因此,您描述的症状。

通过保持对标题元素的引用并在其后插入由showSubcategoryItems(data) 组成的任何内容,这很容易克服。

function showSingleCategory (subheader) {
    // keep reference to the appended <h3>
    var $header = $("<h3 class=\"subcat\">" + subheader.name + "</h3>").appendTo("#gallery");

    $.ajax({
        url: "http://localhost/mysite/wp-json/wp/v2/posts?categories=" + subheader.id + "",
        dataType: "json",
        contentType: "GET",
        success: function(data) {
            // Show items in subcategory after the corresponding header
            $(showSubcategoryItems(data)).insertAfter($header),
        }
        error: handleAjaxError
    });
}

if (subheaders.length > 0) { //If there are subcategories
    for (i = 0; i < subheaders.length; i++) {
        showSingleCategory(subheaders[i]);
    }
}

showSubcategoryItems() 将是这种一般形式:

function showSubcategoryItems(data) {
    var html = .....; // compose html from data
    return html;
}

通过同步附加标头,无论接收 ajax 响应的顺序如何,它们的顺序都可以保证与原始 subheaders 一致。

【讨论】:

    【解决方案2】:

    尝试在您的成功回调中添加&lt;h3&gt; inside。这样,它将同时附加每个标题作为其各自的项目,为您提供所需的输出。

    function showSingleCategory (subheader) {
        $.ajax({
          url: "http://localhost/mysite/wp-json/wp/v2/posts?categories=" + subheader.id + "",
          dataType: "json",
          contentType: "GET",
    
          success: function (data) {
            $("#gallery").append($("<h3 class=\"subcat\">" + subheader.name + "</h3>"));
            showSubcategoryItems(data);
          }, // Show items in subcategory
    
          error: handleAjaxError
       });
    }
    
    if (subheaders.length > 0) { //If there are subcategories
       for (i = 0; i < subheaders.length; i++) {
         showSingleCategory(subheaders[i]);
       }
    }
    

    【讨论】:

    • 有效!非常感谢您编写解决方案!这对我帮助很大!
    • @AlexanderPrisazhny 没问题!很高兴它有帮助 =)
    • 是的,只是不能保证 ajax 响应以与请求相同的顺序返回。如果保留原始订单很重要,那就不要这样做。
    • @AlexanderPrisazhny 您应该检查 Roamer 的解决方案 - 它应该是公认的答案。
    • @mhodges,你是一个善良大方的人。
    猜你喜欢
    • 2018-07-31
    • 1970-01-01
    • 2017-10-01
    • 2016-08-28
    • 1970-01-01
    • 2021-04-15
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多