I am using an Ajax script to get data from my database and post it into multiple textboxes. After posting the data, I also want to calculate with the textboxes.
When I run the script, I see that the script runs all the calculations at the same time. Does someone know how I can build multiple onSuccess
functions in my script so the script executes the codes in the right order?
Here is my script:
$(document).on('change', '[id^=vat1]', function getVat12() { // Do an Ajax request to retrieve the product price
console.log("getVat2 before ajax", jQuery('#vat1').val());
jQuery.ajax({
url: './get/get2.php',
method: 'POST',
data: {'id' : $('#vat1').val()},
success: function(response){
// and put the price in text field
console.log("getPrice after ajax", jQuery('#vat1').val());
jQuery('#percentage1').val(response);
// code 1
var numVal1 = Number(document.getElementById('quantity1').value);
var numVal2 = Number(document.getElementById('price_ex1').value);
var totalValue1 = numVal1 * numVal2
document.getElementById('totalprice1').value = totalValue1.toFixed(2);
//code 2
var numVal3 = Number(document.getElementById('totalprice1').value);
var totalValue2 = numVal3;
document.getElementById('subtotal').value = totalValue2.toFixed(2);
//code 3
var numVal4 = Number(document.getElementById('totalprice1').value);
var numVal5 = Number(document.getElementById('percentage1').value);
var totalValue3 = numVal4 / 100 * numVal5
document.getElementById('vat2').value = totalValue3.toFixed(2);
},
error: function (request, status, error) {
alert(request.responseText);
},
});
});
If you are familiar with promises you can achieve the same by doing the following.
$(document).on('change', '[id^=vat1]', function getVat12() {
// Do an Ajax request to retrieve the product price
console.log("getVat2 before ajax", jQuery('#vat1').val());
jQuery.ajax({
url: './get/get2.php',
method: 'POST',
data: {
'id': $('#vat1').val()
},
success: function (response) {
// and put the price in text field
console.log("getPrice after ajax", jQuery('#vat1').val());
jQuery('#percentage1').val(response);
calculateTotalPrice().then(function(res) {
calculateSubTotal().then(function(res1) {
calculateTotalFinal().then(function(res2) {
console.log('All Done');
});
});
});
},
error: function (request, status, error) {
alert(request.responseText);
},
});
});
function calculateTotalPrice() {
return new Promise(function(resolve, reject) {
setTimeout(function(){
var numVal1 = Number(document.getElementById('quantity1').value);
var numVal2 = Number(document.getElementById('price_ex1').value);
var totalValue1 = numVal1 * numVal2
document.getElementById('totalprice1').value = totalValue1.toFixed(2);
resolve('totalSet');
}, 0)
});
}
function calculateSubTotal() {
return new Promise(function(resolve, reject) {
setTimeout(function(){
var numVal3 = Number(document.getElementById('totalprice1').value);
var totalValue2 = numVal3;
document.getElementById('subtotal').value = totalValue2.toFixed(2);
resolve('subTotalSet');
}, 0)
});
}
function calculateTotalFinal() {
return new Promise(function(resolve, reject) {
setTimeout(function(){
var numVal4 = Number(document.getElementById('totalprice1').value);
var numVal5 = Number(document.getElementById('percentage1').value);
var totalValue3 = numVal4 / 100 * numVal5
document.getElementById('vat2').value = totalValue3.toFixed(2);
resolve('finalAmountSet');
}, 0)
});
}
Based on your question:
Does someone know how I can build multiple onSuccess functions in my script so the script executes the codes in the right order?
then yes, you can create multiple onSuccess functions. Taken from the jQuery ajax documentation :
As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn.
Based on that documentation, you could pass an array of callbacks to perform which will be called in the order given. For your code, this could be an implementation of such:
$(document).on('change', '[id^=vat1]', function getVat12() { // Do an Ajax request to retrieve the product price
console.log("getVat2 before ajax", jQuery('#vat1').val());
jQuery.ajax({
url: './get/get2.php',
method: 'POST',
data: {'id' : $('#vat1').val()},
success: [code1, code2, code3],
error: function (request, status, error) {
alert(request.responseText);
},
});
});
function code1(response){
// and put the price in text field
console.log("getPrice after ajax", jQuery('#vat1').val());
jQuery('#percentage1').val(response);
// code 1
var numVal1 = Number(document.getElementById('quantity1').value);
var numVal2 = Number(document.getElementById('price_ex1').value);
var totalValue1 = numVal1 * numVal2
document.getElementById('totalprice1').value = totalValue1.toFixed(2);
}
function code2(response){
//code 2
var numVal3 = Number(document.getElementById('totalprice1').value);
var totalValue2 = numVal3;
document.getElementById('subtotal').value = totalValue2.toFixed(2);
}
function code3(response){
//code 2
var numVal3 = Number(document.getElementById('totalprice1').value);
var totalValue2 = numVal3;
document.getElementById('subtotal').value = totalValue2.toFixed(2);
}
In regards to:
When I run the script, I see that the script runs all the calculations at the same time
This, as Quentin pointed out, is not the case. None of the functions performed are asynchronous, which means it should be executed in a top-down approach.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.