Simple analysis of $.getjson asynchronous request and synchronous request _javascript skill

Source: Internet
Author: User

Let's talk about the problems I've been having. One of my previous functions wanted to call the return value of the previous function, but its return value was always empty, and then I turned over some data to understand that the asynchronous request was at mischief, not to mention, look at the example, this is the code for the return value function I had before:

function Get_no_order_array () { 
  var order_info = Show_order (); 
  var order = []; 
  
  $.getjson (".. /json/customers.json ", function (date) { 
 
    date[' man '].foreach (function (person) { 
      if (Order_info.k_obj[person ] = = = undefined) { 
        order.push (person);}}) 
  ; 
 
  return order; 
} 

It was later found that the printed array order was empty, following the corrected code:

function Get_no_order_array () { 
  var order_info = Show_order (); 
  var order = []; 
  $.ajaxsettings.async = false;//plus $.ajaxsettings.async = false before execution;  (Synchronous execution) 
  $.getjson (".. /json/customers.json ", function (date) {//<span style=" line-height:1.5; " >$.getjson don't understand, you can look at my previous blog, or learn about  "JSON" </span> 
date[' man '].foreach (function (person) { 
      if (order_info.k_obj[person] = = undefined) { 
        order.push (person); 
      } 
    }) 
  }); 
  $.ajaxsettings.async = true;//The execution of your code in time to revert to $.ajaxsettings.async = true; (asynchronous execution) return order 
  ; 
} 

Add a $.ajaxsettings.async = False before the loop; Represents a synchronous execution, so that he executes it in sequence.

Simultaneous execution of multiple $.getjson () data confusing problem resolution

Add $.ajaxsettings.async = False before execution; (Synchronous execution)

Return to $.ajaxsettings.async = true in time after executing your code (asynchronous execution)

Otherwise, the code that needs to be executed asynchronously elsewhere.

Example

$.ajaxsettings.async = false;

$.getjson (URL, data, function (data) {});

$.getjson (URL, data, function (data) {});

$.getjson (URL, data, function (data) {});

......

$.ajaxsettings.async = true;

The above analysis of the $.getjson asynchronous request and synchronization request is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.