何时适合使用同步ajax? [英] When is it appropriate to use synchronous ajax?
问题描述
我刚刚读了另一个关于jQuery同步ajax调用的问题,我想知道:
I was just reading another question about jQuery's synchronous ajax call, and I got to wondering:
什么情况下同步版本一个ajax调用有益/必要?
What circumstances make a synchronous version of an ajax call beneficial/necessary?
理想情况下我想要一个例子,为什么同步比标准的ajax好。
Ideally I'd like an example, and why synchronous is better than standard ajax.
推荐答案
我能想到的唯一合理的例子(无法以其他方式解决)是在<$ c $中拨打电话c> window.onbeforeunload ,其中需要它是同步的,否则页面将继续运行,请求永远不会完成。
The only reasonable example I can think of (that can't be worked around another way) is making a call in window.onbeforeunload
, where you need it to be synchronous, or the page will move on and the request will never complete.
在这种使用标准/异步行为的特定情况下,你几乎可以肯定,请求会因为产生任何影响而过早死亡,或者与服务器联系。
In this specific case using standard/asynchronous behavior, you're all but assured the request will die too early to have any impact, or ever contact the server.
我不是说我赞成这样做,恰恰相反(因为它会对用户的浏览速度产生负面影响)。但是......这里没有多少选择。
I'm not saying I'm in favor of doing this, quite the opposite (as it negatively impacts the user's browsing speed). But...there's not much option here.
这篇关于何时适合使用同步ajax?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!