How does Javascript-ajax achieve asynchrony?

Source: Internet
Author: User
Keywords javascript html5 node.js php
Ajax method source code I have seen, is the Ajax method calls a callback, first execute the Ajax method and then execute the callback, which is different from the normal reference function? So the async callback of Ajax is in the experience where;

Reply content:

Ajax method source code I have seen, is the Ajax method calls a callback, first execute the Ajax method and then execute the callback, which is different from the normal reference function? So the async callback of Ajax is in the experience where;

The top few really irrelevantly replying.
First, the browser's JS engine is single-threaded, and performing a time-consuming operation must hinder the execution of the thread's subsequent code (such as waiting for a response from the network request). Some languages solve this problem by opening a sub-thread and putting the time-consuming action on the child thread.
The JS engine itself does not support multi-threading, but the browser basically has three threads: JS engine thread, Event trigger thread, HTTP request thread. After the two threads are triggered, the corresponding callback function is placed in the execution queue of the JS engine thread for queuing, as long as the JS engine thread is idle and then executes the callback function added to the queue in turn. Of course, the execution of these callback functions is still blocking the thread.

The page does not refresh the data request, returns the data through the JS Operation Dom, the user experience is good; simply call the JS function only to the front-end operation, no interaction with the server; Ajax is a combination of technology to enhance the user experience, do not have to refresh the entire page every request, that is, JS Operation Dom for local refresh

In my opinion, there is no difference in nature, an incoming function, a similar object-oriented I polymorphic overrides (perform default or custom overrides)

  1. References
    such as:
    //Get User Data
    Function getDate () {
    }

    //Login
    Function Login (CB) {

     //Login logic ...//Login successful after execution callback reference if (CB && typeof cb = = = ' function ') {CB (); }  

    }

    Login (getDate);

  2. Ajax can be implemented internally (not sure it is, just simulate it)

    function XMLHttpRequest () {
    }

    You can have a default or no, not when the send callback is executed.
    XMLHttpRequest.prototype.onreadystatechange = function () {};

    XMLHttpRequest.prototype.get = function (URL) {

       this.url = url;

    };

    XMLHttpRequest.prototype.send = function () {

       // 发送http请求,修改readyState等   // 从this.url获取数据   // 适时调用onreadystatechange, 没有默认onreadystatechange时判空   if (this.onreadystatechange &&       typeof this.onreadystatechange === 'function') {       this.onreadystatechange();   }

    };

    var ajax = new XMLHttpRequest ();
    Ajax.get (URL);
    Ajax.onreadystatechange = function () {

       // 覆盖默认的,或者没有默认的话重新定义

    };
    Ajax.send ();

You can see the difference when you switch to a 2G network. The following code will continue if the request is slow. Wait for synchronization.

  • 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.