This:
Code:
$.post('/other/api/get-discount/', {product_id: product_id, price_list_id: PriceListId, price: qty}, function(response){
if(response){
$("#discount_"+ival).val(response);
}
});
Is an asynchronous (means that the rest of the script can continue, even if there's no answer yet) request (AJAX). The alert creates a pause whereby the request has time to be processed. The alternative is to perform everything that is dependent upon there being a response within the callback function. In this case I think that would be like so:
Code:
function xyz(ival){
var qty = parseInt($('#qty_'+ival).val());
var unitPrice = parseInt($('#unitPrice_'+ival).val());
var id = "select#product_id_"+ival+" option:selected";
var PriceListId = $('#price_list_id_'+ival).val();
var Tax = $('#tax_'+ival).val();
var product_id = $(id).val();
if(unitPrice!=0){
$('#total_'+ival).val(qty * unitPrice);
$.post('/other/api/get-discount/', {product_id: product_id, price_list_id: PriceListId, price: qty}, function(response){
if(response){
$("#discount_"+ival).val(response);
}
var total = parseInt($('#total_'+ival).val());
if(total!=0){
var discount = $('#discount_'+ival).val();
if(discount!=0){
$('#tad_'+ival).val(total - ((total * discount)/100));
$('#netTotal_'+ival).val(total - ((total * discount)/100));
$('#grandTotal').val($('#netTotal_'+ival).val());
}else{
$('#tad_'+ival).val(total - 0);
$('#netTotal_'+ival).val(total - 0);
$('#grandTotal').val($('#netTotal_'+ival).val());
}
}
if(Tax!=0){
var tad = parseInt($('#tad_'+ival).val());
var tax = parseInt($('#tax_'+ival).val());
if(tax!=0){
$('#netTotal_'+ival).val(tad + ((tad * tax)/100) );
$('#grandTotal').val($('#netTotal_'+ival).val());
}
}
$( "#tax_"+ival ).keyup(function() {
var tad = parseInt($('#tad_'+ival).val());
var tax = parseInt($('#tax_'+ival).val());
if(tax!=0){
$('#netTotal_'+ival).val(tad + ((tad * tax)/100) );
$('#grandTotal').val($('#netTotal_'+ival).val());
}
});
});
}
}
In an unrelated matter (regardless of the above change), this appears unwise:
Code:
$( "#tax_"+ival ).keyup(function() {
var tad = parseInt($('#tad_'+ival).val());
var tax = parseInt($('#tax_'+ival).val());
if(tax!=0){
$('#netTotal_'+ival).val(tad + ((tad * tax)/100) );
$('#grandTotal').val($('#netTotal_'+ival).val());
}
});
This should either be assigned separately, outside of the xyz function. Or the keyup event should be removed from this element before attaching, just in case this function has run previously on that element. Namespacing the event would be a good idea at that point as well so as not to conflict with any other potential keyup events. This is done differently (unbind or off) depending upon the version of jQuery being used. Further, the change event is usually (though not always) preferable to the keyup for form values.
Bookmarks