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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:02:30+00:00 2026-06-16T03:02:30+00:00

When I do this: $.ajax({ url: purl, dataType: ‘json’, success: function(data){ alert(data.length); } });

  • 0

When I do this:

$.ajax({
    url: purl,
    dataType: 'json',
    success: function(data){
        alert(data.length);
    }
});

I get an alert with the length count.

but when I do this…

$.ajax({
    url: purl,
    dataType: 'json',
    success: function(data){
        items = data.length;
    }
});

alert(items);

Setting length to global varialbe, it doesn’t want to play nice. Thoughts?

  • 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-16T03:02:32+00:00Added an answer on June 16, 2026 at 3:02 am

    The AJAX portion doesn’t happen instantaneously; the alert takes place before the AJAX callback has time to set the new value for items.

    As of jQuery 1.5, $.ajax returns an object that implements the Promises interface. As such, you can call the .done method anytime you need to be sure that the promise has resolved prior to executing your code:

    ​var promise = $.ajax({/*...*/});
    
    promise.done(function(data){
        // Only works if the promise is resolved
    });
    

    As an example, suppose we created a deferred object directly:

    var promise = $.Deferred();
    

    We then told it not to resolve until after five seconds:

    setTimeout(function(){ 
        promise.resolve("Foo");
    }, 5000);
    

    But then we bound a handler to respond to clicks on the document object:

    $(document).on("click", function () {
        promise.done(function(data){
            alert("Data is " + data);
        });
    });
    

    Note that we are putting our alert within an anonymous function, and passing it to promise.done to be executed only after the promise has been resolved.

    This builds up a queue until that promise is resolved. If I click four times in those first five seconds, nothing will happen, but as soon as that promise resolves I will see four alert boxes appear, one after another.

    Now that the promise is resolved, any further clicks (after the 5 second wait) will result in immediate action.

    Demo: http://jsfiddle.net/w7swE/1/

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

Sidebar

Related Questions

Could someone tell me if this: $.ajax({ url: 'test.html', success: function(data) { alert(Data Loaded:
I have this JSON Ajax function $.ajax({ url: ?page= + page, dataType: json, success:
I have this: $.ajax({ url: blah.php?something, dataType: json .... success: function(x){ for(i in x.artists)
I have a jQuery AJAX function like this: $.ajax({ url: 'crud/clients.php', dataType: 'json', type:
I have this ajax function $.ajax({ url: requestUrl, type: 'get', dataType: 'html', beforeSend: function()
I need to make this data variable global: $.ajax({ url: get_data.php, cache: false, dataType:
I have a jQuery AJAX call with type:'GET' like this: $.ajax({type:'GET',url:'/createUser',data:userId=12345&userName=test, success:function(data){ alert('successful'); }
I have a function: $(.delete).click(function() { $.ajax({ url: ServerHandler.ashx, data: mode=delete&item= + $(this).attr(title), success:
Basically, I have an AJAX request like this: $.ajax({ url: http://192.168.0.100/, success: function(x) {
I have some very simple sample code like this: $.ajax({ url: 'demo2.htm', success: function(loadeddata){

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.