我试图按特定顺序执行函数,但在理解 $.when()
时遇到了问题。
function x() {
def = $.Deferred();
$.when(def).then(console.log(def.state()));
}
x();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
我的理解是,当 def
被解析时,console.log(def.state())
应该执行。换句话说,由于 def
处于 pending
状态,所以在执行 x()
时我不应该得到任何结果吗?
最佳答案
then
需要回调,因此您应该创建一个包装函数:
function x() {
def = $.Deferred();
$.when(def).then(function(){
console.log(def.state())
});
}
x();
关于javascript - $.when.then() 执行的 jQuery 序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42375939/