AJAX ASYNC错误与真实 [英] AJAX ASYNC False vs. True
问题描述
我在这里有一个测试站点(kdmalikdesign.com/test/rsd/index.html).我正在用它做很多事情.我主要担心的是,除非ASYNC为FALSE(除非我听说这是错误的做法),否则它现在不起作用?
现在我已经确定async为false的原因是因为当我触发成功回调时,不会加载xml数据,但是当我加载完成的回调时,一切都可以正常加载.我必须将其更改为async false,才能使其与成功回调一起正常工作.
是否有执行此操作的特定方法?基本上,ajax调用所做的是获取一个xml文件并根据文件名读取该文件,并使用特定数据填充页面.我基本上是在练习/锻炼.
谢谢你, 卡姆龙
Running a synchronous call is usually a malpractice, as you are effectively running a request while losing all the benefits for asynchronicity, when you could be using callbacks to do the same thing in an asynchronous fashion.
async:false
will cause the jQuery.ajax()
call to block until it returns. Effectively, in pseudocode, instead of this:
function ajax:
perform request
callback with results
You are doing this:
function ajax:
perform request
while (no results) wait
return results
This completely blocks the execution of anything else until this is over...which is pretty horrible. The obvious use case for it is running stuff in a waterfall
pattern: task 1 -> task 2 -> task 3
, which can happen.
If you can afford to block your browser, still consider using callbacks. They will allow you to keep the rest of your site active and well, while processing stuff. You can easily do this by setting async:true
and providing a callback with your next step. You may end up in a callback spaghetti, however, and may want to use a library to manage large operations if you have them.
A very good candidate for this hails from Node.JS and is called async.js
. It is the tool for MapReduce stuff, and implements both waterfall
and parallel
running models.
Morale of the story: async:false can 100% of the time be replaced with a callback.
这篇关于AJAX ASYNC错误与真实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!