You have a race condition.
You have multiple ajax requests in progress at the same time. New requests get started before previous requests complete. Here’s the problem.
Lets say The user is searching for “the grave”. Since you use the keyup event, you pretty much send a request for each successive letter combo in that string.
When a request for "the " gets sent, there’s going be a lot of results. It might take lets say 300ms for your server to produce the list because it’s large.
The next request which happens say 50ms later, for “the g” will produce a much smaller list of results. Your server can find and produce this small list much faster, lets say 100ms.
So, if you do the addition, the fast request for “the g” will be received back by the browser before the slow request for "the ". So, the list for “the g” does display, only for a split second until the larger slower request for "the " finally makes it back, which then gets displayed.
Basically, the order in which the ajax requests complete isn’t dependant on the order they were sent. Your code assumes it is, and this is why it’s having the problem.
You need to manage this. You could abort the other ajax requests before sending a new one. There’s other much more advanced techniques that will maximize responsiveness but make sure the ui reflects something intuitive to the user(google’s autocomplete is pretty nice if you want to look).
There’s some jquery plugins to help though. AjaxQueue and AjaxManager plugins exist. There’s also an Autocomplete plugin as well that probably handles this for you(I’ve never used it).
btw- You might also consider some other modifications.
Right now, it looks like if I search for “t” i get a list of every movie in your db that has a t in the movie title. As your site grows, this list could get very large.
If you really desire this functionality(showing everything), then you don’t even need to any more ajax requests after the first letter is known. You could simply filter the list you got back from the server as the user enters more characters. Of course, if the user enters a new search string, and first letter is no longer a “t”, then a new request needs to be sent.
This could be made to be extremely fast from the user perspective, because you could just pregenerate the lists on your server, optionally just storing them as static files that you periodically update. You just need one list for every unique character in the db.
Another question, I want to add a “close” or “x” icon on the list of results that drops down, as some people will probably not think to click out of the div, how do I go about doing this?
(I’m assuming that you are using JQuery UI 1.8 Autocomplete).
It’s not that they have “errors” per se, but they also entirely fail to handle race-conditions (a significant oversight, IMHO). It’s something you have to manage yourself. I’ve managed to implement it (just a few minutes ago), but my implementation is rough at best.
My “fix” involves implementing a new plugin which is uses ui.autocomplete as the base. However you can probably get away with a handling this in a custom callback function. In a nutshell, you need to keep a handle to the XMLHttpRequest instance returned by jQuery’s ajax request methods. Before a new one is sent out you need to call .abort() on the old one.
here is useful information on aborting a jQuery ajax request that might help:
Sorry my answer is so abstract, my implementation is just too rough to pass on right now.
Edit: Okay, bad assumption on my part… I don’t see the plugin implementation either. maybe you mis-interpreted the suggested plugin as the built-in $.get() method. In any case, the concept on fixing is the same. Hopefully my original post will be of use to other people researching this.