How to make Sweet alert auto close after ajax comp

2019-08-27 22:43发布

I'm using sweet alert, but i want make it close automatically if the request of ajax is completed

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

I am adding a variable

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

and make swal like this

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
},

but still not like I expected

1条回答
成全新的幸福
2楼-- · 2019-08-27 22:51

I recommend you to use SweetAlert2 - the supported fork of original SweetAlert.

What you looking for is onOpen parameter and .showLoading() method, here's your task:

Swal.fire({
  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@8"></script>

PS. notice that SweetAlert2 is a little bit different from SweetAlert, check the simple migration guide: https://github.com/sweetalert2/sweetalert2/wiki/Migration-from-SweetAlert-to-SweetAlert2

查看更多
登录 后发表回答