Conflict in executing events based on change fields

Hello;

I need when setting the percentage discount, then to calculate automatically the discounted amount and the grand total, and when setting the discounted amount then to calculate automatically the grand total and to clear (or calculate) the percentage discount. The conflict in below events that each one is going to run the other, how I can overcome this?

ConflictEvents

frappe.ui.form.on(‘Trip Order’, ‘discounted_percentage’, function(frm, cdt, cdn) {

frm.set_value(‘discounted_amount’, frm.doc.total_price * frm.doc.discounted_percentage/100);
frm.set_value(‘grand_total’, frm.doc.total_price - frm.doc.discounted_amount);
})

frappe.ui.form.on(‘Trip Order’, ‘discounted_amount’, function(frm, cdt, cdn) {

frm.set_value(‘grand_total’, frm.doc.total_price - frm.doc.discounted_amount);
frm.set_value(‘discounted_percentage’, frm.doc.grand_total / frm.doc.total_price);
})

Regards
Bilal