Прежде чем показывать миксин SweetAlert2 Toastr, дождитесь завершения всех вызовов ajax - PullRequest
0 голосов
/ 27 сентября 2019

Я использую приведенный ниже код для отображения уведомления после ajax-запроса


const Toast = Swal.mixin({ toast: true, position: 'top-end', showConfirmButton: false, timer: 3000 });

//ajax call
$.ajax({
    .....
    success: function(response){
        reloadpanel1(response.id);   //another ajax call
        reloadpanel2(response.id);   //another ajax call
        Toast.fire({
            type: 'success',
            title: response.message,
            customClass: { popup: 'adjust' }
        })
    }
})

Проблема в том, что уведомление всплывает еще до того, как reloadpanel1 и reloadpanel2 завершат свои запросы.Есть ли способ, при котором Toastr не сработает, если все вызовы AJAX еще не завершены?

РЕДАКТИРОВАТЬ:

$(document).ajaxStart()/.ajaxStop() не будет, так как сообщение уведомления зависит от jsonresponse.message значение

1 Ответ

0 голосов
/ 27 сентября 2019

Пришлось попробовать асинхронное ожидание

const Toast = Swal.mixin({ toast: true, position: 'top-end', showConfirmButton: false, timer: 3000 });

//ajax call
$.ajax({
    .....
    success: async function(response){
        await reloadpanel1();   //another ajax call
        await reloadpanel2();   //another ajax call
        Toast.fire({
            type: 'success',
            title: response.message,
            customClass: { popup: 'adjust' }
        })
    }
})
...