As part of a project I’ve been working on recently I chose to implement a jQuery UI autocomplete bar. Now, due to the application I have heavily customized it. One of these customizations is something that I call ‘dynamic delay’. So what is it? Basically the delay time varies depending on how many characters have been typed into the input box. As the user’s query becomes more specific, the delay dies off. So if you enter in “a”, it will wait a second until it returns any results. If you type in “as” it will have a 0.5s delay. I think it makes for a nicer user experience (than simply having a minimum character input). Also, from the ajax I only return the Top 25 rows, that way the load is never too high on the database. Click below for a demo, or read after the break to see how I do it. For best effect type “harry” slowly into the textbox in the demo … you’ll see the shortening delay.
What we’re doing here is doing the autocomplete as per usual with a delay of 1 second. Then we add a handler for the keydown method of the searchBox and when the key is pressed you divide the delay by the length of the input box.
Let me know what you think and if this makes for a nicer user experience. Try fudge around with the numbers too. Maybe even a different scaling algorithm? Feel free to use the code wherever you like!