How to make Sweet alert auto close after ajax complete?

若如初见. 提交于 2019-12-11 22:03:24

问题


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:


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



来源:https://stackoverflow.com/questions/48877076/how-to-make-sweet-alert-auto-close-after-ajax-complete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!