t4t5 / sweetalert

A beautiful replacement for JavaScript's "alert"
https://sweetalert.js.org
MIT License
22.4k stars 2.84k forks source link

sweet alert - swal.showLoading not working #778

Closed Kepron closed 6 years ago

Kepron commented 6 years ago

My codes,

$("#ex").submit(function(e) {           
    swal({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        type: 'warning',
        showCancelButton: true,
        allowOutsideClick: false,
        allowEscapeKey: false,
        allowEnterKey: false,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes',
        cancelButtonText: 'No'
        }).then((result) => { 
        if (result.value)
        {
            swal.showLoading()
            $.ajax({
                url: 'example.php',
                type: 'POST',
                data: $('#ex').serialize(),
                dataType: "json",
                success: function(result){
                    swal.hideLoading()
                    swal(
                        'Deleted!',
                        'Your file has been deleted.',
                        'success'
                    )
                }
            });
        }
    });
    e.preventDefault();
});

swal.showLoading() and swal.hideLoading() is not working. What is the problem?

Kepron commented 6 years ago

I found the solution.

$("#ex").submit(function(e) {           
    swal({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        type: 'warning',
        showCancelButton: true,
        allowOutsideClick: false,
        allowEscapeKey: false,
        allowEnterKey: false,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes',
        cancelButtonText: 'No'
        }).then((result) => { 
        if (result.value)
        {
            swal({
                title: 'Please Wait..!',
                text: 'Is working..',
                allowOutsideClick: false,
                allowEscapeKey: false,
                allowEnterKey: false,
                onOpen: () => {
                    swal.showLoading()
                }
            })
            $.ajax({
                url: 'example.php',
                type: 'POST',
                data: $('#ex').serialize(),
                dataType: "json",
                success: function(result){
                    swal.hideLoading()
                    swal(
                        'Deleted!',
                        'Your file has been deleted.',
                        'success'
                    )
                }
            });
        }
    });
    e.preventDefault();
});