I have an input number surrounded by two buttons (-/+) in order to add or remove quantity from this input. Right after the quantity, I have a submit button which launches an ajax code from a module that updates the quantity of the product in a cart.
How can I trigger this button after like 1.5s after the user stops clicking/changing the values from this input (to avoid multiple calls), and intercept when the ajax call is done, so that the user can reuse the input?
This is how I’m increasing the value of the input and started to work with. I can’t find the logic to do it after increasing the value…
let currentAdding = false;
$(document).on('click', '[data-action="more"]', function() {
let input = $(this).siblings('input[type="number"]');
let val = input.val();
let total = parseInt(val) + parseInt(1);
input.val(total)
if (currentAdding === false) {
$(this).parents('form').find('input[type="submit"]').trigger('click') //this click trigger an ajax call from a module
//set currentAdding after the ajax is done
currentAdding = true;
}
});
2
Answers
I've ended up with this solution :
You can use something called debouncing in javascript.
Create a function called debounce
now you can modify your event handler something like this
you can read more about debouncing here