I asked a question How to fire Ajax request after user inactivity in 1.5 seconds on how to update user data after user has finished typing instead on every key up, the I was referred to a post where they recommended using javascript debounce, which I love How to implement debounce fn into jQuery keyup event? but I can't seem to find a way to implement it into my code, this is what the solution I was referred to said I should do
$('input').keyup(debounce(function(){
var $this=$(this);
//alert( $this.val() );
var n1 = $this.val();
var n2 = $('#n2').val();
var n3 = $('#n3').val();
var calc = n1 * n2 * n3;
alert(calc);
},500));
my function works perfectly but I dont want it running every time, so I don't know how to implement debounce in this case, this is the code that I want to add debounce to
//cart.php
<input type="text" class="count_cart content_to_cart oninput qty-remove-defaults" onkeyup="setcart(this)" onkeydown="autoSave(this)" id="updates_486" >
function autoSave(e){ //JS where I want to implement debounce
setTimeout(function(){
var getsizes_response = $(e).attr("id");
var getsave_response_quantity = $("#"+getsizes_response).val();
$.ajax({
type: "POST",
url: "functions.php",
data: {getsizes_response: getsizes_response, getsave_response_quantity: getsave_response_quantity},
cache: false,
timeout: 5000,
success: function(r){
$("#ajax_responses_get_positioner").show();
$("#ajax_responses_get_positioner").html(r);
},
error: function(){
$("#ajax_responses_get_positioner").show();
$("#ajax_responses_get").html("Error saving order, please reload page and try again.");
$("#ajax_responses_get").css({'background': 'red', 'display': 'block'});
$("#ajax_responses_get_positioner").delay(6000).fadeOut("fast");
}
});
}, 1500);
}