1

Let's say I have an input like this

<input type="text" id="userhere">

What I want to do is to fire Ajax request when user finishes typing and is inactive for 1.5 sec. This is what I have tried to do, but it seems to fire every time the user inputs data every time, I want when the user finishes typing, then the data fires Ajax request only once not every time the user is typing.

<input type="text" id="userhere" oninput="autoSave(this)"> 
//and oninput is not cross browser effiecient
function autoSave(e){
  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);
      }
    }); 
  }, 1500);
}

How do I fire the Ajax when user has finished typing?

Azametzin
  • 5,223
  • 12
  • 28
  • 46
Shasha
  • 439
  • 8
  • 26
  • 1
    listen (addEventListener) for keystrokes, set a timeout on each keystroke (clearing the previous one) ... timeout will trigger if not cleared - perhaps the "listen for keystrokes" part is redundant for this code – Jaromanda X Mar 22 '20 at 22:31

0 Answers0