16

I have been playing a little with the SweetAlert plugin: Sweet alert

I wanted to make a delete button, where the user gets prompted before the actual delete. When the user then presses "delete" again, is then says "done" and the user has to click "ok" again for the prompt to go away for good.

SweetAlert has a timer function, so you can auto-close that last "Done" prompt after a few seconds or so, which works just fine. They also have a feature, where you can implement a function to be run when the user clicks "OK" on the "Done" prompt. Problem is, that function is not ran if the prompt auto closes after the timer is done.

Any ideas how this can be done?

With timer and function not being run:

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

Without timer, but with a working function (when clicking the "ok" button):

swal("Deleted!", "Your row has been deleted.", "success"), function () {
    location.reload();
    tr.hide();
};
Teilmann
  • 2,146
  • 7
  • 28
  • 57

5 Answers5

29

Explanation

I think you have to take the swal apart from the function. What I mean is that the swal is displayed, the function runs on the background and the modal automatically closes.

Javascript/jQuery:

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

Your code with an example of 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");
        }
    });
Hkidd
  • 872
  • 10
  • 25
12

Cant you just use then?

This way is much cleaner.

swal({
    title: 'Login Success',
    text: 'Redirecting...',
    icon: 'success',
    timer: 2000,
    buttons: false,
})
.then(() => {
    dispatch(redirect('/'));
})
Sonson Ixon
  • 385
  • 1
  • 5
  • 17
  • this works perfectly. Except, depend on version of swal, you should use `showConfirmButton:false, showCancelButton:false` – Tuan Jinn May 03 '20 at 14:48
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);
ibnɘꟻ
  • 830
  • 13
  • 15
  • Nice!!!! I was looking this for hours and you got it just this simple! Thank you very much!! – Evis Feb 09 '18 at 08:21
5

solution is here.

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

See. A message with auto close timer

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')
  }
})

My code

swal.fire({ type: 'success', title: 'Saved.', 
            showConfirmButton: false, timer: 1500 
}).then((result) => {
    if (result.dismiss === Swal.DismissReason.timer) {
         $("#new_reminder").modal("hide");                            
    }
});
Limon Monte
  • 52,539
  • 45
  • 182
  • 213
TodsaHerb
  • 51
  • 1
  • 3
3

This issue was fixed with new release of Sweetalert 2

see Plunker

Plunker

.
eli
  • 8,571
  • 4
  • 30
  • 40