2017-09-15 29 views
2

ajaxを呼び出して新しいhtml要素を描画するには、ajaxStop関数を使用する必要がありますが、ajax関数をfinsihedした後、ajaxを繰り返し繰り返して繰り返します。 ajaxStop関数。 jQueryのコード:それはあなたの関数を呼び出しを停止する必要があるときに、これまでAjaxStop関数内のAjax関数

$(document).ajaxStop(function() { 
      $.ajax({ 
       type: 'post', 
       url: url.build('test/payment/getmethod'), 
       cache: false, 
       showLoader: true, 
       method: "POST", 
       success: function(data) { 
       if(data!='') { 
        var htm =''; 
        htm += '<div class="payment-method-billing-address">'; 
        $.each(data , function(i, method) { 
        htm += '<input type="radio" class="required" name="snap-method" value='+method.value+'">'+method.label+'<br>'; 
        }); 
        htm += '</div>'; 
        $(htm).insertBefore(".checkout-agreements-block"); 
       } 
       return false; 
       } 
      }) 
      return false; 
}); 
+0

ajaxを実行する条件はありません。要求が終了するたびに実行されます。停止する必要があるときにブールを作りなさい。そこには無限ループがありました。 – user5014677

答えて

2
var callAjax= true; 

$(document).ajaxStop(function() { 
      if (callAjax){ 
      $.ajax({ 
       type: 'post', 
       url: url.build('test/payment/getmethod'), 
       cache: false, 
       showLoader: true, 
       method: "POST", 
       success: function(data) { 
       if(data!='') { 
        var htm =''; 
        htm += '<div class="payment-method-billing-address">'; 
        $.each(data , function(i, method) { 
        htm += '<input type="radio" class="required" name="snap-method" value='+method.value+'">'+method.label+'<br>'; 
        }); 
        htm += '</div>'; 
        $(htm).insertBefore(".checkout-agreements-block"); 
       } 
       callAjax = false; 
       return false; 
       } 
      }); 
      } 
      return false; 
}); 

旗を。ちょうどそこに私は成功方法を追加しました。しかし、あなたが一度だけそれをやる必要があれば、おそらくajaxStopは必要ありませんか?

0

documentationに記載されているように、global: falseでAJAXコールを実行すると、ajaxStopイベントを発生させることはできません。

$(document).ajaxStop(function() { 
     $.ajax({ 
      type: 'post', 
      url: url.build('test/payment/getmethod'), 
      cache: false, 
      showLoader: true, 
      global: false, 
      method: "POST", 
      success: function(data) { 
      if(data!='') { 
       var htm =''; 
       htm += '<div class="payment-method-billing-address">'; 
       $.each(data , function(i, method) { 
       htm += '<input type="radio" class="required" name="snap-method" value='+method.value+'">'+method.label+'<br>'; 
       }); 
       htm += '</div>'; 
       $(htm).insertBefore(".checkout-agreements-block"); 
      } 
      return false; 
      } 
     }) 
     return false; 
}); 

しかし、以前のリクエストがすべて完了した瞬間を約束することを約束してください。