Jump to content

Onkeypress delay in AJAX livesearch


alpine

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 ???
Link to comment
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.
Link to comment
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  :)
Link to comment
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*
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.