Javascript 如何监听sweet alert何时关闭

Javascript 如何监听sweet alert何时关闭,javascript,jquery,sweetalert,sweetalert2,Javascript,Jquery,Sweetalert,Sweetalert2,我目前正在使用sweetalert2,并试图检测警报何时关闭。但是,DeleteUnsavedImages函数未启动。我认为将函数分配给onclose键会起作用,但运气不好 swal({ html: data, showCloseButton: false, showCancelButton: false, width: 800, showConfirmButton: false, onClose: Del

我目前正在使用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";
   }

尝试删除(),
onClose:DeleteUnsavedImages
成功!谢谢。哦,@BoyWithSilverWings在评论中给出了解决方案。酷…您没有调用函数
DeleteUnsavedImages
。您正在提供一个函数的引用,然后在警报关闭时调用该函数。是的,它的调用传递了函数的引用,以便在
onClose
触发swal时调用。是的,我知道。但是您的回答是:您只需要调用不带
()
的函数,这是不正确的。你没有调用这个函数。您正在传递对函数的引用。将更新我在回答中的注释,以使概念更清晰。感谢您在@fubar中提到它。使用“then(…)”语句而不是onClose属性还不够吗?请不要只发布代码作为答案,还要解释代码的作用以及它如何解决问题。带有解释的答案通常更有帮助,质量更好,更容易吸引选票。
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";
}