简体   繁体   English

进行异步jQuery调用

[英]Making async jquery calls

$(document).ready(function(){

  $.getJSON("https://api.deckbrew.com/mtg/sets", function(sets) {
    $(sets).each(function() {
      $('<div id="' + this.name + '" class="set"/>')
      .text(this.name)
      .appendTo("#collection");

    });
  });

  $.getJSON("https://api.deckbrew.com/mtg/cards", function(cards) {
    $(cards).each(function(){
      $('<div id="' + this.name + '" class="card"/>')
      .text(this.name)
      .appendTo("#" + this.editions[0].set);

    });
  });

});

I was wondering how I might (without using ajax and sticking to the "getJSON" method) make the two calls happen asynchronously. 我想知道如何(不使用ajax并坚持使用“ getJSON”方法)使这两个调用异步发生。 I can't make anything useful happen with the second jQuery object; 我无法使第二个jQuery对象有用。 I believe that's because of the synchronous nature of the calls. 我相信这是因为通话的同步性。 How can I make them work in order? 如何使它们按顺序工作?

If you want these to happen in order, then you need to specifically serialize them and using the built-in promises that getJSON() returns is a simple way to do that: 如果您希望这些顺序发生,那么您需要专门对其进行序列化,并使用内置的承诺(即getJSON()返回)是一种简单的方法:

$(document).ready(function () {
    $.getJSON("https://api.deckbrew.com/mtg/sets").then(function (sets) {
        $(sets).each(function () {
            $('<div id="' + this.name + '" class="set"/>')
                .text(this.name)
                .appendTo("#collection");
        });
    }).then(function () {
        $.getJSON("https://api.deckbrew.com/mtg/cards").then(function (cards) {
            $(cards).each(function () {
                $('<div id="' + this.name + '" class="card"/>')
                    .text(this.name)
                    .appendTo("#" + this.editions[0].set);
            });
        });
    });
});

Or, a little faster (end to end time) would be to launch both requests at the same time and then process the results in order. 或者,更快一点(端到端时间)将是同时启动两个请求,然后按顺序处理结果。 Again using jQuery promises to manage this: 再次使用jQuery承诺将对此进行管理:

$(document).ready(function(){
    $.when(
        $.getJSON("https://api.deckbrew.com/mtg/sets"), 
        $.getJSON("https://api.deckbrew.com/mtg/cards")
    ).then(function(r1, r2) {
        // process sets
        var sets = r1[0];
        $(sets).each(function() {
          $('<div id="' + this.name + '" class="set"/>')
          .text(this.name)
          .appendTo("#collection");
        });

        // process cards
        var cards = r2[0];
        $(cards).each(function(){
          $('<div id="' + this.name + '" class="card"/>')
          .text(this.name)
          .appendTo("#" + this.editions[0].set);
        });
    });
});

This last scheme uses $.when() to tell us when both ajax calls are done and it also sequences the results for us, regardless of which one actually finished first. 最后一个方案使用$.when()告诉我们两个ajax调用何时完成,并且还为我们排序结果,而不管哪个实际上先完成。

To run the getJSONS's in sequence, run the second in the callback of the first 要依次运行getJSONS,请在第一个的回调中运行第二个

like so 像这样

$(document).ready(function() {
    $.getJSON("https://api.deckbrew.com/mtg/sets", function(sets) {
        $(sets).each(function() {
            $('<div id="' + this.name + '" class="set"/>')
                .text(this.name)
                .appendTo("#collection");
        });
        $.getJSON("https://api.deckbrew.com/mtg/cards", function(cards) {
            $(cards).each(function() {
                $('<div id="' + this.name + '" class="card"/>')
                    .text(this.name)
                    .appendTo("#" + this.editions[0].set);
            });
        });
    });
});

personally, I would go with @jfriend00's promise method - I was going to add that to this answer, but he answered in the meantime, so, go with that more flexible method 就个人而言,我将使用@ jfriend00的promise方法-我将在此答案中添加该方法,但是他同时回答了,因此,请使用该更灵活的方法

EDIT Since you said you were trying to use call both getJSON methods in order, then you can make the second call work after the first by using the DOMNodeInserted event 编辑由于您说过尝试依次使用两个getJSON方法,因此可以通过使用DOMNodeInserted事件使第二个调用在第一个调用之后工作

Well maybe a solution would be to use DOMNodeInserted event since you are appending to #collection 好吧,也许一个解决方案是使用DOMNodeInserted事件,因为您要附加#collection

so: 所以:

$("#collection").on('DOMNodeInserted',function(){

    $.getJSON...
});

According to DOCS 根据DOCS

DOMNodeInserted DOMNodeInserted

Fired when a node has been added as a child of another node. 当一个节点被添加为另一个节点的子节点时触发。 This event is dispatched after the insertion has taken place. 插入发生后调度此事件。 The target of this event is the node being inserted. 此事件的目标是要插入的节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM