11

我一直在玩 SweetAlert 插件:Sweet alert

我想制作一个删除按钮,在实际删除之前提示用户。当用户然后再次按下“删除”时,会说“完成”并且用户必须再次单击“确定”以使提示永远消失。

SweetAlert 具有计时器功能,因此您可以在几秒钟左右后自动关闭最后一个“完成”提示,效果很好。它们还有一个功能,当用户在“完成”提示上单击“确定”时,您可以实现一个要运行的功能。问题是,如果在计时器完成后提示自动关闭,则不会运行该功能。

任何想法如何做到这一点?

timer没有运行和功能:

swal({
     title: "Deleted!",
     text: "Your row has been deleted.",
     type: "success",
     timer: 3000
     },
     function () {
            location.reload(true);
            tr.hide();
     });

没有timer,但具有工作功能(单击“确定”按钮时):

swal("Deleted!", "Your row has been deleted.", "success"), function () {
    location.reload();
    tr.hide();
};
4

5 回答 5

23

解释

我认为你必须swal从功能中分离出来。我的意思是swal显示,功能在后台运行,模式自动关闭。

Javascript/jQuery:

swal({
     title: "Deleted!",
     text: "Your row has been deleted.",
     type: "success",
     timer: 3000
     });
     function () {
        location.reload(true);
        tr.hide();
     };

带有 SweetAlert 示例的代码:

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");
        }
    });
于 2015-04-29T14:22:25.487 回答
9

你不能用then吗?

这种方式干净得多。

swal({
    title: 'Login Success',
    text: 'Redirecting...',
    icon: 'success',
    timer: 2000,
    buttons: false,
})
.then(() => {
    dispatch(redirect('/'));
})
于 2019-03-08T20:33:55.620 回答
5

I found the solution.

Currently I'm experiment using sweetAlert and I found solution for your question.
This is my custom function for creating sweetalert that will close after a few seconds of timer.

var sweetAlert = function(title, message, status, timer = 5000, isReload = false){
    swal({
        title   : title,
        text    : message + '<br/>This pop up will close automatically in <strong class="swal-timer-count">' + timer/1000 + '</strong> seconds...',
        type    : status,
        html    : true,
        timer   : timer,
        allowEscapeKey  : false
    }, function () {
        swal.close();
        if(isReload)
            location.reload(true);
    });
    var e = $(".sweet-alert").find(".swal-timer-count");
    var n = +e.text();
    setInterval(function(){
        n > 1 && e.text (--n);
    }, 1000);
}

You can call this method using this code
Remember, timer is using milisecond.

sweetAlert('Congratulation!', 'You successfully copy paste this code', 'success', 3000, false);
于 2017-07-24T08:13:31.623 回答
4

解决方案在这里。

[https://sweetalert2.github.io/]

看。带有自动关闭计时器的消息

let timerInterval
Swal.fire({
  title: 'Auto close alert!',
  html: 'I will close in <strong></strong> milliseconds.',
  timer: 2000,
  onBeforeOpen: () => {
    Swal.showLoading()
    timerInterval = setInterval(() => {
      Swal.getHtmlContainer().querySelector('strong')
        .textContent = Swal.getTimerLeft()
    }, 100)
  },
  onClose: () => {
    clearInterval(timerInterval)
  }
}).then((result) => {
  if (
    /* Read more about handling dismissals below */
    result.dismiss === Swal.DismissReason.timer
  ) {
    console.log('I was closed by the timer')
  }
})

我的代码

swal.fire({ type: 'success', title: 'Saved.', 
            showConfirmButton: false, timer: 1500 
}).then((result) => {
    if (result.dismiss === Swal.DismissReason.timer) {
         $("#new_reminder").modal("hide");                            
    }
});
于 2019-09-04T07:21:16.853 回答
3

此问题已在Sweetalert 2的新版本中得到修复

见 Plunker

普朗克

.
于 2015-04-29T14:03:38.547 回答