Javascript-ajax Asynchronous and synchronous problems

Source: Internet
Author: User
Keywords javascript php
We ask the great gods what is Ajax async, what is Ajax synchronization, what is the difference between the two, or what is the difference between the two applications.

Reply content:

We ask the great gods what is Ajax async, what is Ajax synchronization, what is the difference between the two, or what is the difference between the two applications.

What's the relationship between synchronous async and Ajax?
Ajax is the abbreviation for asynchronous Javascript and XML
It's an asynchronous way of talking about synchronization.

Asynchronous is the current thread request operation after the new thread has finished processing, callback results to the current thread

Synchronization means that when you execute this program, you can't go on to execute other code, put it on Ajax, it's like, when you get a message during Ajax, nothing can be done, it's not scientific, so async is to get him to run other parts of the code when it gets, Then callback the callback to process the result.

In a series of event queues that need to be executed:
Synchronous operation: Requires the compiler to execute a piece of the statement, if an event has not been completed, then the system will need to wait until the event is completed!
Asynchronous operation: Just like this event is pulled out to the other team, doing a separate thing, the program executes to this asynchronous time, will be divided into two parts, part of the execution of the following program, the other part will be executed independently of this asynchronous, non-impact program loading!
In the user's perspective, this does not make the user feel that he is waiting for a response!

In the Ajax.open method, the 3rd parameter is set to synchronous or asynchronous. Prototype such as JS class library is generally default to asynchronous, that is set to true.
First of all, in the case of synchronization, JS will wait for the request to return, get status. You do not need the onReadyStateChange event handler function. Asynchronous, however, requires onreadystatechange event handling, and a value of 4 correctly handles the following.

My own understanding is that, Ajax synchronization, that is, the request to the server and wait for the server's response, until the server responds to continue to go down; Ajax asynchronous, that is, after sending a request to the server, no need to wait for the server's response, go straight back.

Let me give you an example.
Let's say we have three sentences.

    1. METHOD1 () takes 0.5 seconds

    2. $.ajax () takes 2 seconds

    3. METHOD2 () takes 0.5 seconds

Synchronization: is 1 after execution, execution 2, blocking until the end of Ajax, execution method 31 total 3 seconds
Async: After executing 1, 2 starts executing at the same time, 3 also immediately executes non-blocking, go down. Theory all over altogether 2.5 seconds
Synchronous = serial, asynchronous = parallel, like this.

There is no Ajax asynchronous and synchronous saying that Ajax itself is the meaning of asynchronous request data , but the request data There is a synchronous way.

That is, the XMLHttpRequest object contains two ways to use synchronous requests and asynchronous requests . The same year because of the asynchronous technology to develop a profound impact, so Ajax technology fire, people are also accustomed to the server to request data technology called Ajax, but in fact there is synchronization, but the name is called Ajax synchronization , the conventional

  • Related Article

    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.