AJAX Asynchronization and callback

Source: Internet
Author: User
In a common request/response model, for example, Web 1.0, a client (code running on a browser or local machine) sends a request to the server. The request is synchronous. In other words, the client waits for the response from the server. When the client waits, at least some form of notification will be sent to you:

  • Hourglass (especially on Windows ).
  • Rotate the ball (usually on a Mac machine ).
  • The application is basically frozen, and the cursor changes over a period of time.

This is why Web applications are clumsy or slow-lack real interactivity. When you press the button, the application becomes unusable until the newly triggered request gets a response. If a request needs to be processed by a large number of servers, the waiting time may be long (at least in the world where the multi-processor and DSL are not waiting ).

Asynchronous requestsNoWait for the server to respond. The application continues to run after the request is sent. You can still enter data in a Web form or even exit the form. Applications do not obviously freeze without the rotating ball or hourglass. The server quietly responds to the request and tells the original requester that the work has been completed (the specific method will soon be seen ). The result is that the application feelsNoIt is so slow or slow, but fast response, strong interaction, and much faster. This is only part of Web 2.0, but it is a very important part. All the old GUI components and Web design specifications cannot overcome the slow and synchronous request/response model.

What is the biggest secret between Ajax and Web 2.0? The secret lies inXMLHttpRequestA simple attributeonreadystatechange.

Because it is an asynchronous request, the JavaScript method will not wait for the server. Therefore, the code will continue to be executed, that is, the control will be returned to the form by exiting the method. The user can continue to enter information, and the application will not wait for the server.

This raises an interesting question: what will happen after the server completes the request? If nothing happens, the server is successfullyXMLHttpRequestAfter the request sent to it is processed, some instructions are required to explain how to do it.

NowonreadystatechangeThis attribute is now available. This attribute allows you to specifyCallback Function. Callback allows serversReverse callCode in the Web page. It also gives the server a certain degree of control. After the server completes the request, it will viewXMLHttpRequestObject, especiallyonreadystatechangeAttribute. Then, call any method specified by this property. Callback is called because the server initiates a call to the webpage, regardless of what the webpage is doing. For example, this method may be called when a user is sitting in a chair without touching the keyboard, but it may also be called when the user inputs, moves the mouse, scrolls the screen, or clicks a button. It does not care about what the user is doing.

This is called asynchronous: the user operates the form on one layer, while the server responds to the request and triggersonreadystatechangeThe callback method specified by the property.

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.