ajax的同步和异步使用_何时使用异步或同步AJAX

ajax的同步和异步使用

AJAX, which stands for asynchronous JavaScript and XML, is a technique that allows web pages to be updated asynchronously, which means that the browser doesn't need to reload the entire page when only a small bit of data on the page has changed. AJAX passes only the updated information to and from the server.

AJAX代表异步JavaScript和XML,它是一种允许异步更新网页的技术,这意味着当页面上只有少量数据更改时,浏览器不需要重新加载整个页面。 AJAX仅将更新的信息与服务器之间传递。

Standard web applications process interactions between web visitors and the server synchronously. This means that one thing happens after another; the server does not multitask. If you click a button, the message is sent to the server, and the response is returned. You cannot interact with any other page elements until the response is received and the page is updated. 

标准的Web应用程序同步处理Web访问者和服务器之间的交互。 这意味着一件事情接连发生。 服务器不执行多任务。 如果单击按钮,则消息将发送到服务器,并返回响应。 在收到响应并更新页面之前,您无法与任何其他页面元素进行交互。

Obviously, this kind of delay can negatively affect a web visitor's experience — hence, AJAX.

显然,这种延迟可能会对Web访问者的体验(即AJAX)产生负面影响。

什么是AJAX? ( What Is AJAX? )

AJAX is not a programming language, but a technique that incorporates a client-side script (i.e. a script that runs in a user's browser) that communicates with a web server. Further, its name is somewhat misleading: while an AJAX application might use XML to send data, it could also use just plain text or JSON text. But generally, it uses an XMLHttpRequest object in your browser to request data from the server and JavaScript to display the data.

AJAX不是一种编程语言,而是一种结合了与Web服务器通信的客户端脚本(即在用户浏览器中运行的脚本)的技术。 此外,其名称在某种程度上具有误导性:尽管AJAX应用程序可能使用XML发送数据,但也可能仅使用纯文本或JSON文本。 但是通常,它使用浏览器中的XMLHttpRequest对象从服务器请求数据,并使用JavaScript显示数据。

AJAX:同步或异步 ( AJAX: Synchronous or Asynchronous )

AJAX can access the server both synchronously and asynchronously:

AJAX可以同步和异步访问服务器:

  • Synchronously, in which the script stops and waits for the server to send back a reply before continuing.

    同步 ,其中脚本停止并等待服务器发送回回复,然后继续。

  • Asynchronously, in which the script allows the page to continue to be processed and handles the reply if and when it arrives.

    异步 ,其中脚本允许页面继续进行处理,并在到达页面时和到达时处理答复。

Processing your request synchronously is similar to reloading the page, but only the requested information is downloaded instead of the entire page. Therefore, using AJAX synchronously is faster than not using it at all — but it still requires your visitor to wait for the download to occur before any further interaction with the page can proceed. People know that they sometimes need to wait for a page to load, but most folks are not used to continued, significant delays after they are on a site. 

同步处理您的请求类似于重新加载页面,但是仅下载请求的信息,而不下载整个页面。 因此,与完全不使用AJAX相比,同步使用AJAX的速度要快得多,但是它仍然需要您的访问者等待下载完成,然后才能与页面进行任何进一步的交互。 人们知道有时候他们需要等待页面加载,但是大多数人不习惯在网站上之后持续大量的延迟。

Processing your request asynchronously avoids the delay while the retrieval from the server takes place because your visitor can continue to interact with the web page; the requested information will be processed in the background and the response will update the page as and when it arrives. Further, even if a response is delayed — for example, in the case of very large data — site visitors may not realize it because they are occupied elsewhere on the page.

异步处理您的请求避免了从服务器进行检索时的延迟,因为您的访问者可以继续与网页进行交互。 所请求的信息将在后台处理,响应将在页面到达时更新页面。 此外,即使响应被延迟(例如,在数据非常大的情况下),站点访问者也可能不会意识到,因为他们被页面上的其他地方所占据。

Therefore, the preferred way to use AJAX is to use asynchronous calls wherever possible. This is the default setting in AJAX. 

因此,使用AJAX的首选方法是尽可能使用异步调用。 这是AJAX中的默认设置。

为什么要使用同步AJAX? ( Why Use Synchronous AJAX? )

If asynchronous calls provide such an improved user experience, why does AJAX offer a way to make synchronous calls at all?

如果异步调用提供了这样的改善的用户体验,那么为什么AJAX完全提供了进行同步调用的方法?

While asynchronous calls are the best choice the vast majority of the time, there are rare situations in which it doesn't make sense to allow your visitor to continue interacting with the web page until a particular server-side process completes.

尽管异步调用在大多数情况下是最佳选择,但在极少数情况下,不允许访问者继续与网页进行交互直到特定的服务器端过程完成才有意义。

In many of these cases, it may be better to not use AJAX at all and instead just reload the entire page. The synchronous option in AJAX is there for the small number of situations in which you can't use an asynchronous call but reloading the entire page is unnecessary. For example, you might need to handle some transaction processing in which the order is important. Consider a case in which a web page needs to return a confirmation page after the user clicked something. This task requires synchronizing the requests.

在许多情况下,最好完全不使用AJAX,而只是重新加载整个页面。 在少数情况下,您无法使用异步调用,但不必重新加载整个页面,因此AJAX中提供了sync选项。 例如,您可能需要处理一些顺序很重要的事务处理。 考虑一种情况,在这种情况下,网页需要在用户单击某些内容后返回确认页面。 此任务需要同步请求。

翻译自: https://www.thoughtco.com/use-asynchronous-or-synchronous-ajax-2037228

ajax的同步和异步使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值