Table of Contents
回复内容:
Home Backend Development PHP Tutorial javascript - AJAX是怎么样实现异步的呢?

javascript - AJAX是怎么样实现异步的呢?

Jun 06, 2016 pm 08:07 PM
html5 javascript node.js php

AJAX的方法源码我看过,就是AJAX方法里面调用一个回调,先执行AJAX方法再执行回调,这跟普通的的引用函数 有什么区别呢?那么AJAX的异步回调在体验在哪里;

回复内容:

AJAX的方法源码我看过,就是AJAX方法里面调用一个回调,先执行AJAX方法再执行回调,这跟普通的的引用函数 有什么区别呢?那么AJAX的异步回调在体验在哪里;

上面几位真是所答非所问。
首先浏览器的js引擎是单线程的,执行一个耗时操作必定阻碍线程后续代码的执行(比如等待网络请求的响应)。一些语言采用了开一个子线程并把耗时操作放到子线程去执行的办法解决了这个问题。
js引擎本身不支持多线程,但是浏览器基本上都有三个线程:js引擎线程、事件触发线程、http请求线程。后两个线程在触发后会把对应的回调函数放到js引擎线程的执行队列中进行排队等待,只要js引擎线程空闲就会依次执行加入到队列中的回调函数。当然这些回调函数的执行依然是阻碍线程的。

页面无刷新进行数据请求,返回数据通过js操作DOM,用户体验良好;单纯调用js函数仅对前端有操作,没有与服务器的交互;AJAX是组合技术,提升用户体验,不必每次请求都刷新整个页面,即js操作dom进行局部刷新

在我看来本质上没有区别,一个传入函数,一个是类似面向对象中我多态覆盖(执行默认或者自定义覆盖的)

  1. 传引用
    比如:
    // 获取用户数据
    function getDate() {
    }

    // 登陆
    function login(cb) {

    <code>   // 登陆逻辑....
       // 登陆成功后执行回调引用
       if (cb && typeof cb === 'function') {
           cb();
       }</code>
    Copy after login

    }

    login(getDate);

  2. ajax内部可能实现(不确保是这样的,只是模拟一下)

    function XMLHttpRequest() {
    }

    // 可以有默认的也可以没有,没有在send回调执行时判断一下就行了
    XMLHttpRequest.prototype.onreadystatechange = function () {};

    XMLHttpRequest.prototype.get = function (url) {

    <code>   this.url = url;</code>
    Copy after login

    };

    XMLHttpRequest.prototype.send = function () {

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

    };

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

    <code>   // 覆盖默认的,或者没有默认的话重新定义</code>
    Copy after login

    };
    ajax.send();

你换成2G网络就能看出区别了 请求慢的时候下面的代码会继续执行 如果用同步 就干等着吧

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

The Connection Between H5 and HTML5: Similarities and Differences The Connection Between H5 and HTML5: Similarities and Differences Apr 24, 2025 am 12:01 AM

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

HTML5 and H5: Understanding the Common Usage HTML5 and H5: Understanding the Common Usage Apr 22, 2025 am 12:01 AM

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

HTML5: The Building Blocks of the Modern Web (H5) HTML5: The Building Blocks of the Modern Web (H5) Apr 21, 2025 am 12:05 AM

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

H5: Key Improvements in HTML5 H5: Key Improvements in HTML5 Apr 28, 2025 am 12:26 AM

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

See all articles