在甜蜜警报响应后触发一个功能

Trigger a function after a response to sweet alert 2

本文关键字:功能 一个 甜蜜 响应      更新时间:2023-09-26

我已经被这个问题困扰了好几天了。我正在使用sweetalert2,我想要完成的是触发基于给出的答案的javascript函数,所以如果我点击确定,它会触发一件事,如果我点击取消,它会做别的事情。我可以让它工作,但它似乎在甜蜜警报完成之前就触发了功能。下面是一个示例代码:

<script>
    function validateSubmit(a,b,c){
        var mode = a;
        var info = b;
        var trunk = c;
        var iteration = baseName(info);
        if (mode === 'Update' && info != trunk ){
            confirmGetMessage(iteration);
        }
        else {
            alert('seems to work')
        }
    }
    function baseName(str) {
        var base = new String(str).substring(str.lastIndexOf('/') + 1); 
        if(base.lastIndexOf(".") != -1)       
        base = base.substring(0, base.lastIndexOf("."));
        return base;
    }
    function trythis(){
        alert('made it right here!');
    }
    function confirmGetMessage(info){
        var message = "<h3>" + info + "</h3><br/>Or Revert Back to Trunk?";             var contmessage = "Updating " + info;
        swal({
            title: "Update Branch?",
            html: message,
            type: "question",
            showCancelButton: true,
            cancelButtonText: "Switch To Trunk",
            cancelButtonColor: "#0080FF",
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Continue Update",
            closeOnConfirm: false,
            closeOnCancel: false
        }).then(
            function(result){
                swal({
                    text: contmessage,
                    timer: 1400,
                    showConfirmButton: false
                }), alert('work please');
            }, function(dismiss) {
                swal({
                    text: 'Switching to Trunk',
                    timer: 1400,
                    showConfirmButton: false
                });
            }
        );
    }
</script>

所以如果你运行这段代码,警报框会在sweetalert的消息框上方弹出。

---------- ---------------

像这样运行代码似乎更接近了,尽管现在警告消息仍然发生在关闭消息之前,但至少这次我可以看到关闭消息

    function validateSubmit(a,b,c){
            var mode = a;
            var info = b;
            var trunk = c;
            var iteration = baseName(info);
            if (mode === 'Update' && info != trunk ){
                confirmGetMessage(iteration);
            }
            else {
                alert('seems to work')
            }
        }
        function baseName(str) {
            var base = new String(str).substring(str.lastIndexOf('/') + 1); 
            if(base.lastIndexOf(".") != -1)       
            base = base.substring(0, base.lastIndexOf("."));
            return base;
        }
        function trythis(){
            alert('made it right here!');
        }
        function confirmGetMessage(info){
            var message = "<h3>" + info + "</h3><br/>Or Revert Back to Trunk?";
            var contmessage = "Updating " + info;
            swal({
                title: "Update Branch?",
                html: message,
                type: "question",
                showCancelButton: true,
                cancelButtonText: "Switch To Trunk",
                cancelButtonColor: "#0080FF",
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Continue Update",
                closeOnConfirm: false,
                closeOnCancel: false
            }).then(function(){
                swal({
                    text: contmessage,
                    timer: 1400,
                    showConfirmButton: false
                },trythis())
            }, function(dismiss){
                if (dismiss === 'cancel'){
                    swal({
                        text: 'Switching to Trunk',
                        timer: 1400,
                        showConfirmButton: false
                    })
                }
            }
        )}

您在创建警报后的第一个函数调用中将值读入result,但您没有检查调用的值。
从甜蜜警报定时器完成功能,

swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
},
function (isConfirm) {
    if (isConfirm) {
       swal({
          title: "Deleted!",
          text: "Your row has been deleted.",
          type: "success",
          timer: 3000
       });
       function () {
          location.reload(true);
          tr.hide();
       };
    }
    else {
        swal("Cancelled", "Your imaginary file is safe :)", "error");
    }
});

您需要检查输入是否为确认。像这样的东西可能是你想要的,但我没有一个环境来测试它:

swal({
        title: "Update Branch?",
        html: message,
        type: "question",
        showCancelButton: true,
        cancelButtonText: "Switch To Trunk",
        cancelButtonColor: "#0080FF",
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Continue Update",
        closeOnConfirm: false,
        closeOnCancel: false
    },
    function(result){
        if(result)
        {
            swal({
                text: contmessage,
                timer: 1400,
                showConfirmButton: false
            }), alert('work please');
        },      
        else{
            swal({
                text: 'Switching to Trunk',
                timer: 1400,
                showConfirmButton: false
            });
        }
    }
);