javascriptjquerysweetalertsweetalert2

How to listen for when sweet alert closes


I am currently working with sweetalert2 and I am trying to detect when the alert closes. However the DeleteUnsavedImages function is not firing. I thought that assigning the function to the onclose key would work but no luck.

   swal({
       html: data,
       showCloseButton: false,
       showCancelButton: false,
       width: 800,
       showConfirmButton: false,
       onClose: DeleteUnsavedImages()
   }).then(function () {

   });


function DeleteUnsavedImages(){
    var test = "-1";
}

Any help would be appreciated :-)


Solution

  • I tested with my sweet alert to confirm the issue, you just need to pass the function name without () and the function will be called inside onClose event handler of swal. Its called passing a reference of the function to call when onClose gets fired of swal.

    Make a little change like this:

       swal({
           html: data,
           showCloseButton: false,
           showCancelButton: false,
           width: 800,
           showConfirmButton: false,
           onClose: DeleteUnsavedImages        // Removed () from here
       }).then(function () {
    
       });
    
    
       function DeleteUnsavedImages(){
           var test = "-1";
       }