autocomplete through ajax
I want to suggest results using auto-complete. I need to send AJAX requests on each keystroke. For this I want to keep the HTTP connection open for few seconds and if something is typed within that period, I want to send the AJAX in that same connection. If nothing is typed in that period, I want to close the HTTP connection.
Background:
I already use typewatch plugin. But here the HTTP connections are made each time I send a request. I still want to improve the speed. I read in this thread http://www.philwhln.com/quoras-technology-examined#the-search-box that:
Quora uses persistent connections. A HTTP connection is established with the server开发者_如何学JAVA when you start typing the search query.
How can I do this with cross browser support? Is it just keep-alive?
You can't. Each request-response round trip is asynchronous, meaning that when it's sent, it waits for that particular request's response to return, then handles it.
What I think you want to do is prevent your script from hammering the server. To do this there are a variety of methods, but the most common is to use a keystroke timer. The timer waits a specified number of milliseconds after the user finishes typing before sending the request, containing the textbox's value, to the server.
If you're using JQuery you can use the TypeWatch plugin to do this. JQuery will also satisfy your cross browser requirements.
However, since you also want to do auto-complete, you may as well use the JQuery AutoComplete plugin which also has a keystroke timer built in, by default it's set to 400 millseconds. Click the OPTIONS TAB on this page to see what all the configuration options are that you can pass into the plugin.
精彩评论