如何在甜蜜警报关闭时收听

How to listen for when sweet alert closes

我目前正在使用 sweetalert2,我正在尝试检测警报何时关闭。但是 DeleteUnsavedImages 函数没有触发。我认为将函数分配给 onclose 键会起作用,但运气不好。

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

   });


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

如有任何帮助,我们将不胜感激:-)

我用我的 sweet alert 进行了测试以确认问题,您只需要传递不带 () 的函数名称,该函数将在 swal 的 onClose 事件处理程序中调用。当 onClose 被 swal 解雇时,它调用传递函数的引用来调用。

像这样做一点改变:

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

   });


   function DeleteUnsavedImages(){
       var test = "-1";
   }
swal({
     html: data,
     showCloseButton: false,
     showCancelButton: false,
     width: 800,
     showConfirmButton: false,
     onClose: () => {
         this.DeleteUnsavedImages();
     }
})

private DeleteUnsavedImages(){
}
swal({
    title: "client",
    content: html,
    buttons:
    {
        cancel: {
            text: "Close",
            visible: true,
            closeModal: true,
        },
        confirm: {
            text: "Download",
            visible: true,
            closeModal: false
        }
    },
}).then((confirm) => {
    if (confirm) {
        download();
    }
    else {
        DeleteUnsavedImages();
    }
});

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