search
HomeBackend DevelopmentPHP Tutorialjavascript - ajax 异步和同步的问题

我们问问大神们什么是ajax异步,什么是ajax同步,两者有什么区别,或则两者适用场合有什么不同。

回复内容:

我们问问大神们什么是ajax异步,什么是ajax同步,两者有什么区别,或则两者适用场合有什么不同。

同步异步 和 ajax有什么关系吗
Ajax是Asynchronous Javascript And XML的缩写
本身就是异步的 何谈 同步

异步就是 当前线程请求操作 后天开新线程处理 完毕后 回调结果给当前线程

同步意味着,在执行这段程序的时候,不能接着执行其他代码,放在Ajax上,就相当于,当你Ajax获取消息期间什么都干不了,一看就不科学,所以异步是为了让他在获取时可以正常运行其他部分的代码,然后callback回调处理结果就行了。

在一系列需要执行的事件队列里面:
同步操作: 需要编译器对语句一件件的执行下去,如果某个事件还没有执行完毕,那么系统就需要一直等待,直到该事件执行完毕!
异步操作: 就好比吧这个事件拉出来放到另外一队,做一个单独的事情,程序执行到这个异步的时候,会分成两部分,一部分继续执行下面的程序,另一部分会单独去执行这个异步,互不影响程序加载!
在用户角度来说,这样不会让用户感觉他在等待响应!

ajax.open方法中,第3个参数是设同步或者异步。prototype等js类库一般都默认为异步,即设为true。
先说下同步的情况下,js会等待请求返回,获取status。不需要onreadystatechange事件处理函数。而异步则需要onreadystatechange事件处理,且值为4再正确处理下面的内容。

我自己的理解是这样的,ajax同步,即向服务器请求并在此等待服务器的回应,直到服务器回应了才继续往下走;ajax异步,即向服务器发送请求后,无需等待服务器的回应,直接往后走。

举个例子说明吧。
我们假设有三句语句

  1. method1() 需要0.5秒

  2. $.ajax() 需要2 秒

  3. method2() 需要0.5秒

同步:就是1执行完后,执行2的时候,阻塞,直到ajax全部结束,执行方法3 一共3秒
异步:执行完1后,2开始执行的同时,3也马上执行非阻塞,往下走。 理论全部结束 一共 2.5秒
同步=串行,异步=并行,这样子。

本来没有ajax异步和同步的说法,AJAX本身便是异步请求数据的意思,但是请求数据还有同步的方式。

即XMLHttpRequest对象包含了同步请求异步请求两种使用方式。当年因为异步技术给开发带来了深刻影响,所以AJAX技术火了,人们也习惯把向服务器请求数据的技术称为AJAX,但实际上还有同步,只是称呼上就被叫做了AJAX同步了,约定俗成而已

Statement
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
PHP vs. Python: Understanding the DifferencesPHP vs. Python: Understanding the DifferencesApr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: Is It Dying or Simply Adapting?PHP: Is It Dying or Simply Adapting?Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The Future of PHP: Adaptations and InnovationsThe Future of PHP: Adaptations and InnovationsApr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

When would you use a trait versus an abstract class or interface in PHP?When would you use a trait versus an abstract class or interface in PHP?Apr 10, 2025 am 09:39 AM

In PHP, trait is suitable for situations where method reuse is required but not suitable for inheritance. 1) Trait allows multiplexing methods in classes to avoid multiple inheritance complexity. 2) When using trait, you need to pay attention to method conflicts, which can be resolved through the alternative and as keywords. 3) Overuse of trait should be avoided and its single responsibility should be maintained to optimize performance and improve code maintainability.

What is a Dependency Injection Container (DIC) and why use one in PHP?What is a Dependency Injection Container (DIC) and why use one in PHP?Apr 10, 2025 am 09:38 AM

Dependency Injection Container (DIC) is a tool that manages and provides object dependencies for use in PHP projects. The main benefits of DIC include: 1. Decoupling, making components independent, and the code is easy to maintain and test; 2. Flexibility, easy to replace or modify dependencies; 3. Testability, convenient for injecting mock objects for unit testing.

Explain the SPL SplFixedArray and its performance characteristics compared to regular PHP arrays.Explain the SPL SplFixedArray and its performance characteristics compared to regular PHP arrays.Apr 10, 2025 am 09:37 AM

SplFixedArray is a fixed-size array in PHP, suitable for scenarios where high performance and low memory usage are required. 1) It needs to specify the size when creating to avoid the overhead caused by dynamic adjustment. 2) Based on C language array, directly operates memory and fast access speed. 3) Suitable for large-scale data processing and memory-sensitive environments, but it needs to be used with caution because its size is fixed.

How does PHP handle file uploads securely?How does PHP handle file uploads securely?Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

What is the Null Coalescing Operator (??) and Null Coalescing Assignment Operator (??=)?What is the Null Coalescing Operator (??) and Null Coalescing Assignment Operator (??=)?Apr 10, 2025 am 09:33 AM

In JavaScript, you can use NullCoalescingOperator(??) and NullCoalescingAssignmentOperator(??=). 1.??Returns the first non-null or non-undefined operand. 2.??= Assign the variable to the value of the right operand, but only if the variable is null or undefined. These operators simplify code logic, improve readability and performance.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use