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

  • Home
  • SEARCH
  • 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 8963959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:28:34+00:00 2026-06-15T16:28:34+00:00

The variable ajaxdata is modified within the success function, if that hasn’t been done

  • 0

The variable ajaxdata is modified within the success function, if that hasn’t been done yet, I would like to wait 2 seconds, then continue without it.

The use case is for a jqueryui autocomplete field. The autocomplete source is an ajax request, but if the user types quickly, and exits the field before the list loads, the field remains unset. Using the ‘change’ event on the autocomplete I check if the user entered a valid option without selecting it, but this doesn’t work if the source hasn’t loaded when the change event fires. So I would like to put a delay in the change function which waits, if the source (stored in the variable ‘ajaxdata’) is empty.

code:

input.autocomplete({
            source: function (request, response){
                    $.ajax(
                        {
                            type: "GET",
                            url: "/some/url",
                            dataType: "json",
                            success: function(data){
                                response($.map(data,function(item){
                                    return{
                                        label: item.label,
                                        value: item.value
                                    }
                                }));
                                ajaxdata = data;
                            }
                        }
                    );
                    // ajaxopts = ajaxsource(request,response,ajaxurl,xtraqry)
                },                   
            change: function(event, ui) {
                if (!ui.item) {
                    // user didn't select an option, but what they typed may still match
                    var enteredString = $(this).val();
                    var stringMatch = false;
                    if (ajaxdata.length==0){
                        ///  THIS IS WHERE I NEED A 2 SECOND DELAY
                    }
                    var opts = ajaxdata;
                    for (var i=0; i < opts.length; i++){
                        if(opts[i].label.toLowerCase() == enteredString.toLowerCase()){
                            $(this).val(opts[i].label);// corrects any incorrect case
                            stringMatch = true;
                            break;
                        }
                    }
            }
            },
        });

Edit:

To be more specific about the problem: This delay needs to be conditional. Meaning that if the data is already loaded (either because it came from a static source, or from an earlier ajax call) I do not want to have a delay.

  • 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-06-15T16:28:35+00:00Added an answer on June 15, 2026 at 4:28 pm

    If I’m understanding you properly, I think you just want to check and see if ajaxdata has been populated; but if it hasn’t, only wait two more seconds and then just proceed without it.

    Try this:

    change: function(event, ui) {
        if (!ui.item) {
            // user didn't select an option, but what they typed may still match
    
            if (ajaxdata.length==0){
                ///  THIS IS WHERE I NEED A 2 SECOND DELAY
    
                //pass in 'this' so that you can use it
                setTimeout(function() {correctCase(this);}, 2000);
            }       
        }
    }
    

    . . . . .

    function correctCase(inThis){       
    
        //I'm not sure what this variable does.  do you really need it???   
        var stringMatch = false;
    
        var enteredString = $(inThis).val();
        //you still want to be sure that ajaxdata is not empty here
        if (ajaxdata.length==0){
            var opts = ajaxdata;
            for (var i=0; i < opts.length; i++){
                if(opts[i].label.toLowerCase() == enteredString.toLowerCase()){
                    $(inThis).val(opts[i].label);   // corrects any incorrect case
                    stringMatch = true;  //this variable doesn't seem to do anything after this???
                    break;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before variable-length arrays were supported, I would dynamically allocate them like this: int foo(size_t
num is a variable which is dynamically changed within 1 to 9. I must
I have the following variable: pageID = 7 I'd like to increment this number
Variable $d comes from file_get_contents function to a URL. $answer = @new SimpleXMLElement($d); Below
Variable $name (string) gives something like (5 possible values): Elton John Barak Obama George
Variable $name (string) gives something like (possible values): Elton John 2012 George Bush Julia
I have a perl variable $results that gets returned from a service. The value
If I have a variable in C# that needs to be checked to determine
By variable format I mean that each row in the text file begins with
Block variable in objective-c is a reference and I learned from somewhere infer that

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.