There is a problem with the implementation of two svit alerts in a row. The page displays a page with a list of cars. Double-click on a line to receive a sweet alert that asks the user if he really wants to place an order. If the user clicks OK, then the ajax request is sent to the server and executes certain logic for the purchase of the car. Then it returns the necessary information to me and if everything is successful then I look in the table for the car that was bought, the column quantity in stock and decrease the number by one. After that I call another 1 alert which tells the user that he has successfully completed the order. If, for example, the number of cars in stock is 0, then the user is displayed the corresponding message in the alert. So when you first start the application, everything works out successfully, and on further attempts without restarting the application, everything works, but the last (successful or unsuccessful) alert does not pop up. I tried to pull these alerts into a separate function and called them with setInterval, at least with a delay of 100 milliseconds, and everything works out well. Please tell me what's wrong? Why without delay does not work out normally?

Here is my code without delay and with it:

$('tbody').dblclick(function (e) { swal({ title: 'Оформление заказа', text: 'Вы уверены что хотите оформить заказ?', type: "info", showCancelButton: true }, function (isConfirm) { if (isConfirm) { var carId = $(e.target).closest('tr').attr('id'); $.ajax({ type: "POST", url: "/Home/CarPurchase", data: { "carId": carId }, success: function (response) { if (response.result) { var count = $(".context-menu tbody tr[id=" + response.id + "] td[id=count]"); count.text(count.text() - 1); swal({ title: "Заказ оформлен", type: "success" }); } }, error: function (xhr) { swal({ title: xhr.responseText, type: "warning" }); } }); } } ); }); $('tbody').dblclick(function (e) { swal({ title: 'Оформление заказа', text: 'Вы уверены что хотите оформить заказ?', type: "info", showCancelButton: true }, function (isConfirm) { if (isConfirm) { var carId = $(e.target).closest('tr').attr('id'); $.ajax({ type: "POST", url: "/Home/CarPurchase", data: { "carId": carId }, success: function (response) { if (response.result) { var count = $(".context-menu tbody tr[id=" + response.id + "] td[id=count]"); count.text(count.text() - 1); setTimeout(Success, 100); } }, error: function (xhr) { swal({ title: xhr.responseText, type: "warning" }); } }); } } ); }); function Success() { swal({ title: "Заказ оформлен", type: "success" }); } 

1 answer 1

One of the assumptions is that usually, if the swal is called in the callback function of another swal, then the first swal may not have time to work to the end, and the call to the internal swal will not follow. You can try to add for the first option closeOnConfirm: false to avoid this.

  swal({ title: 'Оформление заказа', text: 'Вы уверены что хотите оформить заказ?', type: "info", showCancelButton: true, closeOnConfirm: false, }, function (isConfirm) {..} 
  • Denis thanks, helped. - Andrei