Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax function inside ajaxStop function

I need to use ajaxStop function to call an ajax to render new html element, but after the ajax function finsihed it the ajax repeat itself over and over again, i just need to run this ajax once inside the ajaxStop function. jQuery code:

$(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;
 });
like image 914
Hunter Avatar asked Jun 30 '26 01:06

Hunter


1 Answers

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;
 });

Flag it when ever it needs to stop calling your function. Right there i just added in the success method. But if you need to only do it once you maybe don't need ajaxStop?

like image 53
user5014677 Avatar answered Jul 02 '26 13:07

user5014677



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!