jquery - ajax链式调用完成

标签 jquery ajax

我有三个功能。我想在功能二(功能一之后)和功能三完成后做一些事情。我该怎么办?

function one(){
    $.ajax({
      url:'url'
    }).done(function(res){
      two();
    })
}


function two(){
    $.ajax({
      url:'url'
    })
}

function three(){
    $.ajax({
      url:'url'
    })
}

enter image description here

最佳答案

从每个函数返回 $.ajax promise ,然后您可以使用 $.when()

function one() {
  return $.ajax({
    url: 'url'
  }).then(two);      
}

function two() {
  // return request promise
  return $.ajax({
    url: 'url'
  })
}

function three() {
  // return request promise
  return $.ajax({
    url: 'url'
  })
}

$.when(one(),three()).then(function(){
   //all three requests completed here
})

关于jquery - ajax链式调用完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50382530/

相关文章:

javascript - 我怎样才能把这个javascript变成一个函数?它已经是一个函数了吗?关于js结构的几个很基础的问题

javascript - 如何将我的 Ajax 处理移动到一个函数中?

javascript - 发布时收到空请求

php - 如何在magento中动态改变价格值

javascript - 如何选择在另一个属性中具有特定值的标记的 XML 属性值?

javascript - 访问当前 div 之外的 div 内的元素

javascript - 在页面上创建弹出窗口时,Home 和 End 键不起作用?

函数内的javascript函数不起作用?

javascript - 与 observable 的 knockout 依赖

python - 如何在ajax中使用django url?