ajax完成后如何使Sweet alert自动关闭?

sai*_*jie 1 php ajax sweetalert sweetalert2

我正在使用甜蜜警报,但如果 ajax 的请求完成,我想让它自动关闭

swal({
  title: "Are you sure?",
  text: "You are choosing order by custom search",
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Confirm",
  closeOnConfirm: false,
  timer: **AUTO WITH AJAX??**
},
Run Code Online (Sandbox Code Playgroud)

我正在添加一个变量

var dones = $(document).ajaxComplete(function(){
                            swal.close()
                        });
Run Code Online (Sandbox Code Playgroud)

并像这样制作 swal

swal({
  title: "Are you sure?",
  text: "You are choosing order by custom search",
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Confirm",
  **timer: dones,**
  closeOnConfirm: false
},
Run Code Online (Sandbox Code Playgroud)

但仍然不像我预期的那样

Lim*_*nte 5

我建议您使用SweetAlert2 - 原始 SweetAlert 的支持分支。

你要找的是onOpen参数和.showLoading()方法,这是你的任务:

Swal.fire({
  title: 'I will close automatically when AJAX request is finished',
  didOpen: function () {
    Swal.showLoading()
    // AJAX request simulated with setTimeout
    setTimeout(function () {
      Swal.close()
    }, 2000)
  }
})
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
Run Code Online (Sandbox Code Playgroud)

附注。注意 SweetAlert2 和 SweetAlert 有点不同,查看简单迁移指南:https : //github.com/sweetalert2/sweetalert2/wiki/Migration-from-SweetAlert-to-SweetAlert2