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 6647459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:32:25+00:00 2026-05-26T00:32:25+00:00

I know that it works. I have implemented it successfully. $(#vehicle_application_product_id).autocomplete({ minLength: 2, source:

  • 0

I know that it works. I have implemented it successfully.

$("#vehicle_application_product_id").autocomplete({
      minLength: 2,
      source: function(request, response) {
          var term = request.term;
          if (term in cache) {
              response(cache[term]);
              return;
          }

          lastXhr = $.getJSON("/products/get_json_list", request, function(data, status, xhr) {
              cache[term] = data;
              if (xhr === lastXhr) {
                  response(data);
              }
          });
      }
  });

I am like a script kiddie with some of this stuff. I would NEVER be able to write that anonymous function myself, because of the following reasons:

1) How did this person know to put the parameters “request” and “response” in the spec?

2) The lines beginning “lastXhr = ” is really mystifying. There is yet another anonymous function at the end. I realize that is probably a callback, something that is executed after the request is sent to the specified URL.

3) That last conditional in the anonymous function:

if ( xhr === lastXhr ) {                                                                                      
    response( data );
}

What the heck is that? If then third parameter, xhr, from this anonymous block is equal to the vaue returned by the .getJSON call ENCOMPASSING this anonymous block, return the data in the response?

That is really the most difficult part to wrap my head around. And I notice that’s 3 equals signs. This looks like pretty advanced code concepts, I would just appreciate what the rationale behind this is so that I can become a better coder.

  • 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-26T00:32:26+00:00Added an answer on May 26, 2026 at 12:32 am
    1. The parameters request and response will be passed to the function assigned to the source option as documented here. In essence, the plugin promises to pass these two parameters to the callback and that’s how you signal that you do want to get hold of them.

    2. That function is a callback scheduled to be invoked when the request completes, not after it’s simply sent. Also, when calling getJSON, the code assigns the resulting object to the lastXhr variable so that it remembers which was the last request that it made.

    3. Inside the callback, you have a reference to the request that completed in the form of the parameter xhr. So that if is equivalent to writing:

      if (the request that completed is the last request made) {
          // ...
      }
      

      Why do this? To make sure that you do not act on the results of the request if, due to the response taking too long to come back and the user typing too fast, you have multiple outstanding requests. In such a scenario, you are really not interested in the results of any but the last one you made.

      How does the callback have access to the variable lastXhr from an outer scope? This is due to closure.

    4. What’s with the three equal signs? That’s the identity operator.

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

Sidebar

Related Questions

I have successfully implemented a restful service that works with my own client. Now,
I have a C program that I know works on 32-bit systems. On 64-Bit
I know I have that my connection to the database works, and a test
Does anyone know if theres a jquery autocomplete library that works similar to the
I have implemented c2dm and it works fine that the device receives the message
I know that the following works but it is not that readable, is there
I want to know that how voice recognition in android works? Which library it
Does anyone know of a library that works with Ruby 1.9.1 on Rails 3
I know that C-k C-c comments a selection, and currently it works as thus
I want to know that how does form based authentication works in weblogic. My

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.