如何在ajax完成后使甜蜜警报自动关闭?

时间:2018-02-20 02:41:16

标签: 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??**
},

我正在添加一个变量

var dones = $(document).ajaxComplete(function(){
                            swal.close()
                        });

并像这样制造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
},

但仍然不像我预期的那样

1 个答案:

答案 0 :(得分:1)

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

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

swal({
  title: 'I will close automatically when AJAX request is finished',
  onOpen: function () {
    swal.showLoading()
    // AJAX request simulated with setTimeout
    setTimeout(function () {
      swal.close()
    }, 2000)
  }
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@7"></script>

PS。请注意,SweetAlert2与SweetAlert略有不同,请查看简单的迁移指南:https://github.com/sweetalert2/sweetalert2/wiki/Migration-from-SweetAlert-to-SweetAlert2