Jump to content


Photo

Onkeypress delay in AJAX livesearch


  • Please log in to reply
4 replies to this topic

#1 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 23 July 2006 - 10:13 AM

I have a livesearch in ajax handling queries to a rather big mysql table containing rows of products.
What i am experiencing some problems with is that the browser occaitionally freezes (firefox,ie etc - doesn't matter) probably due to problems handling the query results as the customer enters characters in the text field.
Now i have set it up so that it's needed at least two chars to actually start searching - but what i think would do things a little easier is to delay each keypress (onkeydown,keypress,keydown) so that the search holds a second or so before sending the http request just to limit the number of queries.

I played around with my javascript at first, but the best solution i've come up with so far is to add usleep(500000); on top of the php script handling the db-queries to hold on 0.5 sek before starting.

Can anyone point out a better solution than this ???

#2 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 24 July 2006 - 04:28 PM

Actually, live search (with the result showing under the search field) is not usually a good feature. It's resource hog and no one really use it. If you want that feature, I would recommend you set a php file to put the top couple of hundreds search queries in an javascript array, and use that array to show the top searches on key press. This will speed up the list update and won't use a lot of resources.

NoGray.com


#3 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 25 July 2006 - 02:00 PM

Thanks for your thought, i still think i am going to stick with the query on keypress and try and optimize it the best i can as i have near 300.000 rows on products in the db and it seems impossible to find a pattern on most searched items.

I think i will try to limit the onkey search to maximum 100 results in standard and make an optional more results button and see if that helps - actually it is working very nice and is actually quite fast too - better and better as i move along and continue fine-tuning it  :)

#4 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 25 July 2006 - 09:44 PM

btw - i use onkeypress in different terms to suit browsers, but what is to be preferred ? - onkeypress or onkeyup ???
Javascript is a relative new language to mee so i am in a heavy learning cycle now, getting used to the var instead of $ etc *lol*

#5 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 25 July 2006 - 11:16 PM

onkeypress is better since it's supported by most browsers (I think). And you can find out users searches by recording the search terms and how many times. Usually people look for similar products when they visit a site.

NoGray.com





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users