为什么同步XMLHttpRequest被视为已弃用? [英] WHY is synchronous XMLHttpRequest considered as deprecated?

查看:663
本文介绍了为什么同步XMLHttpRequest被视为已弃用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经知道同步请求和异步请求之间的区别,例如下面的解释: 同步和异步请求

I already know the difference between synchronous and asynchronous requests, like explained here for example: Synchronous and asynchronous requests

当您尝试进行同步XMLHttpRequest时(例如,以firefox表示),您会收到以下著名警告:

When you try to make a synchronous XMLHttpRequest (with firefox for example), you get this famous warning:

由于以下原因,不赞成在主线程上使用同步XMLHttpRequest: 对最终用户体验的有害影响

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience

但是,我认为您可以以积极的方式使用同步方面,例如在页面加载期间,获取一些加载过程几乎不依赖的重要数据(没有这些内容,内容就可以根本不会显示).在那里,用户必须等待请求很自然;它的行为就像请求的内容是调用文件的真实部分一样,必须与文件的其余部分一样读取.

However, I think you can use the synchronous aspect in a positive manner, for example during the page load, to get some important data on which the loading process depends hardly (without which the content can't be displayed at all). There, it's quite natural that the user has to wait for the request; it behaves like if the request's content would be a real part of the calling file, which would have to be read as well as the rest of the file.

less.js CSS工具似乎很常见:
在任何显示之前,它需要解析.less文件. less团队显然决定选择一个同步请求:我可以想象,如果使用异步请求,则可以在解析较少的文件之前显示该页面,并显示一些原始内容短时间内没有定义任何样式.这不是预期的或方便的行为.因此,同步请求似乎是一个更好的解决方案.

It seems to be pretty much the case for the less.js css tool :
It needs to parse the .less files before any display. The less team apparently decided to choose a synchronous request : I can imagine that with an async request instead, the page could be displayed before the less files would have been parsed, and would display some raw content for a short time, without any style defined. It would not be the an expected or convenient behaviour. So a synchronous request seems to be a better solution there.

警告说:

由于其对最终用户的体验具有不利影响

because of its detrimental effects to the end user's experience

很明显,确保请求尽快运行是开发者的责任,但是如果网站向其自己的服务器(该网站一部分的php文件)发送了强制性请求,或在less.js的情况下,同步行为就是想要的行为.

Obvioulsy, it's the developper's responsability to make sure that the request will run as fast as possible, but in the case of a website sending a mandatory request to its own server (to a php file that is part of the website), or in the case of less.js, the synchronous behaviour is the wanted one.

那么,为什么同步请求被认为已过时,却显得如此有用?

推荐答案

您正在混合使用不同的概念,这就是为什么您想知道为什么不推荐使用它的原因.

You're mixing different concepts which is why you're wondering why it was deprecated.

首先,并不是发明了异步调用来使调用更快.实际上,它们对速度的影响几乎为零.

Firstly, the asynchronous calls were not invented to make calls faster. In fact, they have almost zero impact on the speed.

其次,不是发明异步调用来专门进行必需的或不需要的调用.做出这样的决定是开发人员的责任.

Secondly, the asynchronous calls were not invented to specifically make required or non-required calls. It is the responsibility of the developer to make such a decision.

话虽如此,消息清楚地回答了这个问题:

Having said that, the message answers the question clearly:

由于其对最终用户的体验具有不利影响

because of its detrimental effects to the end user's experience

您的主要困惑是关于异步调用的目标.异步调用背后的整个想法是,以确保用户不必等待结果,而是使应用程序更具响应性.

Your main confusion is about the goal of the asynchronous calls. The whole idea behind asynchronous calls is NOT to ensure that the user will not have to wait for the results, but it is to make the application more responsive.

您提供了页面加载期间所需调用的示例.通过使用异步请求,用户仍然必须等待结果(异步调用的目的不是解决此问题),但是应用程序将保持对用户输入的响应.例如,某些应用程序可以具有[取消]按钮,以便用户可以取消请求.其他一些应用程序可能会显示一些动画或进度条.您(开发人员)可以决定要做什么,但只有异步才能为您提供做出决定的机会.另一方面,同步调用将阻止执行,应用程序看起来像冻结的一样,您(开发人员)在调用期间无法执行任何操作,这是对最终用户体验的 有害影响 邮件正在谈论.

You gave an example of the required call during the page load. By using asynchronous request, the user will still have to wait for the results (the goal of asynchronous calls is not to solve this problem), but the application will stay responsive to the user input. For instance, some applications can have a [Cancel] button so the user can cancel the request. Some other applications may display some animation or progress bar. You, the developer, decide what to do, but only asynchronous gives you this chance to make the decision. Synchronous calls on the other hand will block the execution, the application will look like frozen, you, the developer, cannot do anything during the call, and that's the detrimental effects to the end user's experience the message is talking about.

这篇关于为什么同步XMLHttpRequest被视为已弃用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆