Question: I've build a livesearch with the jQuery.ajax() method. On every keyup events it receives new result data from the server.

The problem is, when I'm typing very fast, e.g. "foobar" and the GET request of "fooba" requires more time than the "foobar" request, the results of "fooba" are shown.

To handle this with the timeout parameter is impossible, I think.

Has anyone an idea how to solve this?

Answer: 
Assign a unique, incrementing ID to each request, and only show them in incrementing order. Something like this:

```
var counter = 0, lastCounter = 0;
function doAjax() {
  ++counter;
  jQuery.ajax(url, function (result) {
    if (counter < lastCounter)
      return;
    lastCounter = counter;
    processResult(result);
  });
}

```
That seems like an intense amount of traffic to send an ajax request for every KeyUp event. You should wait for the user to stop typing - presumably that they are done, for at least a few 100 milliseconds.

What I would do is this:

```
var ajaxTimeout;

function doAjax() {
     //Your actual ajax request code
}

function keyUpHandler() {
    if (ajaxTimeout !== undefined) 
        clearTimeout(ajaxTimeout);

    ajaxTimeout = setTimeout(doAjax, 200);
}

```

You may have to play with the actual timeout time, but this way works very well and does not require any other plugins.

Edit:
If you need to pass in parameters, create an inline function (closure).

```
...
var fun = function() { doAjax(params...) };
ajaxTimeout = setTimeout(fun, 200);

```