Jump to content

Archived

This topic is now archived and is closed to further replies.

alpine

Onkeypress delay in AJAX livesearch

Recommended Posts

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 ???

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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  :)

Share this post


Link to post
Share on other sites
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*

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.