Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SweetAlert2 execute function after timer

What am I doing wrong here?

swal({
    title: "Done.",
    text: "Labels Printed.",
    timer: 1000,
    type: "success",
    showConfirmButton: false

}).then(function () {

    alert('done');

});

The alert is not getting fired, do I need to catch 'timer' somehow? (The alert is just an example, I am actually clearing my form here.)

Also how do I get rid of the textLabels:1 Uncaught (in promise) timer error?

I was using .done()

Can someone add the tag for SweetAlert2? I don't have the reputation to do it.

Mick

What do I need to do when I don't want anything to happen after?:

swal({
    title: "Error.",
    text: "Authorisation Failed.",
    timer: 1000,
    type: "error",
    showConfirmButton: false
}).then(
    function() {}

 )

like this?:

}).then(
        function() {},
        function() {}
       )
like image 610
Mick Avatar asked Oct 30 '25 18:10

Mick


1 Answers

UPDATED (17.11.2017):

Starting from v7.0.0 SweetAlert2 works exactly like was expected by question starter :)


SweetAlert2 uses promises. Each promise can be resolved or rejected and you handling it this way:

swal(…).then(
  function () {
    // handle resolve (confirm button)
  }, 
  function (dismiss) {
    // handle reject, dismiss can be  'cancel', 'overlay', 'close', and 'timer'
  }
)

Closing a modal by timer considered as a promise rejection, so you should handle it like this:

Swal.fire({
  title: 'Auto close alert!',
  text: 'I will close in 2 seconds.',
  timer: 2000
}).then(function() {
  alert('done');
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
like image 106
Limon Monte Avatar answered Nov 02 '25 07:11

Limon Monte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!