Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7531629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:13:24+00:00 2026-05-30T05:13:24+00:00

I’m trying to implement a search box using php and jquery (the seach results

  • 0

I’m trying to implement a search box using php and jquery (the seach results are coming from a database). What I want to do is that as the user type into the search box, a query is made to find the data that contains the keyword the user entered into the search box. The problem I’m having is that I have something like this:

$("#my_search_box").keyup(function(){

    retrieve_results($(this).val());    

});

The problem with doing this is that, the function retrieve_results() is called every time the user is done typing (of course, cause that’s what the keyup is there for). Therefore, this can get very slow because if the user type a word that has like 10 characters, the retrieve_results() function gets called 10 times in a row. Also, I should mention that inside my retrieve_results() function, I’m doing an Ajax request using jQuery $.ajax to get the results. So my question is the following:

How do I make it so that my function retrieve_results() doesn’t get executed everytime the user stops typing (every time the key is up). I want to keep that functionality though, I don’t want the user to click on a “Search” button or something. I just would like my retrieve_results() function to get called only once when the user is done entering all the keywords (not everytime the user enters a letter or something)

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:13:26+00:00Added an answer on May 30, 2026 at 5:13 am

    Ideally, you only want the search to fire for the very last character the user types. The best way to do this is to give him enough time to continually type, and if he pauses (even to think), run the search.
    Using setTimeout by itself won’t work because even that would fire multiple times. You have to clear the timeout by using clearTimeout.

    You can use the following to achieve the desired effect:

    $(document).ready(function() {
        var timeout,
            $search_box = $('#my_search_box');
    
        $search_box.keyup(function() {
            if (timeout) {
                clearTimeout(timeout);
            }
    
            timeout = setTimeout(function() {
                search();
            }, 1000);
        });
    
        function search() {
            var search_txt = $search_box.val();
        }
    });
    

    Here’s what’s happening:
    We assign the setTimeout timer to a variable, which must be declared above your keyup function, and check that variable each time keyup fires. If there’s an existing setTimeout in progress, clear it out so that it won’t fire the search() function and create a new timer set to wait a specified number of milliseconds (1000 in my example).

    See the following JSFiddle for demo: http://jsfiddle.net/highwayoflife/WE4Fr/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.