javascript - 使用 .done 与异步 : false? 同义

标签 javascript jquery ajax asynchronous

我有一大堆 $.ajax 请求需要在触发前相互等待。我知道 async : false 选项已被弃用,而且我也知道如果我将后续函数放在 .done 中,那么它的行为方式与 async : false 相同。然而,为了保持干净,我想知道是否可以作为替代方案,我可以只留下 .done 而无需其他代码,只需从“上到下”排序我的 ajax:这会告诉 jquery 运行 ajax,然后 执行 .done 中的任何操作(在我的情况下什么都没有!),“模仿”异步:false?

举个例子:

$.ajax({type: 'POST',
        data: cool_data : "cool_data",
        url: 'somewhere_over_the_rainbow.php'
        }).done(function(){

        })
$.ajax({type: 'POST',
        data: more_cool_data : "more_cool_data",
        url: 'more_somewhere_over_the_rainbow.php'
        }).done(function(){

        })

谢谢!

最佳答案

没有。

done 注册一个将在服务器响应时调用的回调。

使用 async:false 阻塞页面直到服务器响应。 不要使用它

链接调用的解决方案是使用 then :

$.ajax({ ... })
.then($.ajax({ ... })
.then($.ajax({ ... })
.then(function() { ... finished! ... });

关于javascript - 使用 .done 与异步 : false? 同义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22483919/

相关文章:

jquery - 仅在 div id block 中不存在时才添加输入标签

javascript - 在文本框中按回车键打开链接

python - 动态生成.doc文件

php - laravel 中间件在 ajax 请求 url 中不起作用

javascript - 在离开页面之前完成 Ajax 请求

javascript - 如何构造 if...else 语句以返回 true

javascript - Uncaught ReferenceError : Invalid left-hand side in assignment 'Cart' total calculation

php - 无法使用 Ajax 登录 RESTful 服务器

javascript - Jquery:有什么方法可以让 val() 返回空字符串而不是空列表的 'undefined'?

jquery autocomplete - 如何显示字段 1 + 字段 2 并选择后,只需在文本框中填写字段 1