最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - When is it appropriate to use synchronous ajax? - Stack Overflow

programmeradmin2浏览0评论

I was just reading another question about jQuery's synchronous ajax call, and I got to wondering:

What circumstances make a synchronous version of an ajax call beneficial/necessary?

Ideally I'd like an example, and why synchronous is better than standard ajax.

I was just reading another question about jQuery's synchronous ajax call, and I got to wondering:

What circumstances make a synchronous version of an ajax call beneficial/necessary?

Ideally I'd like an example, and why synchronous is better than standard ajax.

Share Improve this question asked Nov 30, 2010 at 17:16 zzzzBovzzzzBov 179k56 gold badges327 silver badges371 bronze badges 4
  • IMHO never :-) I haven't found a good argument against "find a valid work around" on any situation yet, but I'm sure people on here can prove that wrong – wajiw Commented Nov 30, 2010 at 17:19
  • 3 I'm amused by the fact that the title of your question is logical contradiction. – Nathan Taylor Commented Nov 30, 2010 at 17:31
  • 2 @Nathan Taylor I considered mentioning that AJAX is a misnomer in jQuery. It can be sync or async, xml, json, html, or whatever. In other languages it's typically referred to as a "url request". If we called it that, uninformed outsiders wouldn't get to throw around a new meaningless buzz word. – zzzzBov Commented Nov 30, 2010 at 17:40
  • 1 possible duplicate of Is there any reason to use a synchronous XMLHttpRequest? – Bergi Commented Sep 30, 2013 at 9:07
Add a comment  | 

4 Answers 4

Reset to default 21

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.

In sum, please do not use synchronous requests as @Brandon says: they are a cheap/easy/quick way to avoid making a callback. In addition, modern browsers show warnings if synchronous requests are made and we do not like that. Make your world asynchronous.

synchronous ajax is often used to retrieve a valued from the server which is required to further continue processing of client side code. in such case, the ajax call will block until the call returns with the desired value. example:

a javascript function needs to compute salary for an employee: step1 : get the employee id from the form step2 : make a sync server call passing the emp.id to get his salary/hour step3 : multiply salary rate by number of working hours

as you can see, total salary cannot be computed unless the server call is finished so this should be a sync function, although if using jquery, one could handle onSuccess to compute the salary asynchronously but processing will continue in this if you have a message box to display the salary, it will appear empty...

I would venture a guess that it'd be good in a scenario where you want to perform some ajax calls but you have one call that relies on the results of another call. If you perform them synchronously you can wait for the independent to finish before the dependent call fires.

发布评论

评论列表(0)

  1. 暂无评论