Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sweetalert pause execution and return value

I'd like to replace javascript confirm with sweetalert, in the following code snippet, but I found two limitations, 1) sweetalert does not pause execution like confirm, 2) sweetalert does not return value, boolean, like confirm:

$(document).ready(function(){
  $('#cancelList').click(function(e){   
    e.preventDefault();
    m = confirm($(this).attr('confirm-msg'));
    if (!m){
      return true;
    }
    url = $(this).children('a').attr('href');
......

For returning the Boolean value, I tried to use it like the following:

$(document).ready(function(){
  $('#cancelList').click(function(e){   
    e.preventDefault(); 
    m = false;
    swal({text:$(this).attr('confirm-msg'), type: 'warning',title: '', showCancelButton: true}, function(isConfirm){m = isConfirm; return m;});
    alert('It should not be here before decide in swal');
    if (!m){
      return true;
    }

In the above snippet, the alert is invoked before the sweetalert confirmation appearing!!! and the global variable m seems to be not affected by clicking on OK or Cancel on sweetalert.

What's the problem here? Is there any workaround rather than including the rest of code inside the callback function of sweetalert?

like image 833
SaidbakR Avatar asked Apr 24 '26 14:04

SaidbakR


1 Answers

I had the same problem and found online that sweet alert uses promises. You can use sweet alert as:

Swal.fire({
    /*attributes*/
}).then(function (){
    /*code to execute after alert*/
});

This way the code in the promise block will be executed after clicking the alert button

like image 93
Alexis Tevre Avatar answered Apr 27 '26 05:04

Alexis Tevre



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!