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;
});
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With