0

I have some code in Javascript (not jQuery, unfortunately) at the moment that performs a live search on a database, depending on what the user enters.

The problem is, if you type quickly, it'll still be performing the search from the last keystroke and this can add up to a delay of anything up to ten seconds.

I know I should cache this information, and it's definitely something I'd love to do soon (along with implementing jQuery) but for now I was wondering if there was any way at all to speed this up?

I've already limited the number of rows the search returns to 20, and have made sure the search doesn't run unless there are 3 or more characters.

Thanks

xn dx
  • 793
  • 1
  • 17
  • 28

2 Answers2

2

Make the ajax call trigger 1 or 2 seconds after the user stopped typing.

If you need code for this let me know in the comments.

thwd
  • 23,956
  • 8
  • 74
  • 108
  • 1
    like this one, i did it once like this, used a counter and iniatilize it each time a keystroke was received or a request was made. – Packet Tracer Sep 23 '11 at 11:38
  • This sounds good, thank you - so incorporate that into the JS rather than the PHP? Do you use the setTimeout function? – xn dx Sep 23 '11 at 13:11
  • 1
    Yes, setTimeout. And yes on the client side. – thwd Sep 23 '11 at 13:40
1

You can check if there's already a request running. If so, abort it. (currentRequest.abort())

Snicksie
  • 1,987
  • 17
  • 27
  • 1
    I thought of this too, but the problem with this approach is that if the request already reached the server the database query will still be executed, though not returned it still consumes resources. – thwd Sep 23 '11 at 11:13