Question: I'm sending an ajax request to the server on user's input to an `<input>` element, like this:

```
$('#my-input').bind("input", function(event){
   // here's the ajax request
});

```

What bothers me is that it send unnecessarily many requests on **every** user's keyup, meaning that if the user types very fast, there are many unnecessary requests. So I get the idea that there should be a certain delay/timeout, which waits a certain time (50 miliseconds?) for the user to stop typing before sending the ajax request. That would be one problem solved.

But what about cases when the first ajax request haven't been completed before sending another request? (Typing 60 ms / char while ajax request taking 300 ms).

What is the best way to solve this problem (both idea- and code-based)?

Answer: 
You could just use the setTimeout function. Every so often, see if the text hasn't changed, and if it hasn't, then process accordingly.

```
setTimeout(function() {
      // Do something after 1 second
}, 1000);

```
You can set async: false in your ajax request so it will process second ajax call only after completion of first ajax request.