Ensure that all ajax calls have completed before displaying the SweetAlert2 Toastr mixin

In my code snippet, I am utilizing the following method to display a notification after making an ajax request:


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' }
        })
    }
})

One issue encountered is that the notification appears before both reloadpanel1 and reloadpanel2 complete their respective requests. Is there a way to prevent the Toastr from triggering if all ajax calls have not finished yet?

UPDATE:

The use of $(document).ajaxStart() or .ajaxStop() is inappropriate since the message in the notification relies on the value found in the json response.message.

Answer №1

Just discovered a clever solution to my issue by leveraging the power of the jquery when function

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

//performing an ajax request
$.ajax({
    .....
    success: function(response){
        $.when(reloadpanel1(), reloadpanel2()).done(function(){
            Notification.fire({
               type: 'success',
               title: response.message,
               customClass: { popup: 'adjust' }
            })
        })
    }
})

Answer №2

Need to give async await a shot

const PopUp = SweetAlert.mixin({ popUp: true, position: 'top-end', showConfirmButton: false, timer: 3000 });

//make an AJAX request
$.ajax({
    .....
    success: async function(response){
        await updateSection1();   //another AJAX request
        await updateSection2();   //another AJAX request
        PopUp.fire({
            type: 'success',
            title: response.message,
            customClass: { popup: 'adjust' }
        })
    }
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transform JSON data into React components using asynchronous AJAX requests

I'm facing an issue when trying to convert JSON data into React components using AJAX requests. The JSON file I am working with is located at "src/data/form-inputs.json". The structure of form-inputs.json is as follows: { "form_inputs": [ { ...

Utilize PHP to send the completed form

Forgive my lack of knowledge, but I am new to PHP and recently downloaded a website template that includes a form. I want this form to send an email to my address when the submit button is clicked. How can I achieve this? Below is the existing code snippet ...

What is a method to raise the height when scrolling down without reducing it when scrolling up?

For my One Page project, I want a DIV element to increase in height when scrolling down, but maintain that increased height when scrolling up. Currently, I am utilizing JQuery with the code snippet below: $(function(){ $(window).scroll(function() { ...

Why is it that the edit or delete button is not functioning when I attempt to click on the next page?

Having an issue with my script. It works fine for editing and deleting on the first page, but when I navigate to the next page, those functionalities stop working. Can anyone help me troubleshoot this problem? <script> $(function(){ $(&ap ...

Encountering a 403 error while using the ajax infinite loading script

Based on recommendations from my previous inquiry, I made the decision to incorporate an infinite loading script onto my page. However, every time the script is activated, a 403 - forbidden error occurs. Here is the JavaScript code snippet: jQuery(documen ...

The validation of pre-filled input fields in Angular Material dialogs is not working as expected

I am encountering an issue with a mat-dialog that opens through an edit button within a (mat-)table. Upon opening the dialog, data is passed to populate certain input fields. One of these inputs has validation requiring that it cannot be left empty. The ...

Inconsistency with Mobile Viewport Problem

Apologies for the chaos below, but I've spent quite some time attempting to fix this on my own. It's time to surrender and seek assistance! Here are some screenshots to illustrate the issue: The problem is that sometimes the webpage functions c ...

The data parsing process is throwing an error. Any suggestions on how to fix it?

Utilizing AJAX, I am fetching information from a specified URL. var settings = { "url": ".php", "method": "POST", "timeout": 0, "headers": { "Content-Type": "application/json" }, "data": JSON.stringify({"email":"mail","userid":"admin","typ ...

Problem with the operation of SetInterval/ClearInterval loop

While I am aware that this question has been addressed previously, none of the solutions provided seemed to resolve my specific issue. The problem lies within my timer function which, upon invocation, is supposed to utilize setInterval to run every second ...

Vue.JS has issued a warning indicating that the `util._extend` API is no longer supported. To address this, developers are advised to utilize Object

During the execution of a call API request, the Vue.JS application encountered an error. The API is hosted at Okta, and the request is successful when using cURL in the CLI. Error Message (node:82171) [DEP0060] DeprecationWarning: The `util._extend` API i ...

Handling an Express server that receives a request with no data

I'm struggling with a problem where I am unable to retrieve a basic JSON object. When I log it to the console, all I see is {}. Let me showcase the server code below: const express = require("express"); const app = express(); app.listen(3000); app.us ...

Learn the process of transferring data-target IDs to another JSP using Bootstrap

Currently, I am encountering an issue where I am trying to use bootstrap to display a dialog box by clicking on a button. The dialog box is created in HTML (.jsp) and the submit button is in another .jsp file. However, when I click on the button, instead o ...

formatting a JSON response in an HTML document

Can someone assist me with formatting the json return in html from my jquery json result? Please see below for my html code: $("document").ready(function(){ $(".js-ajax-php-json").submit(function(){ var data = { "action": "testerer" }; ...

Utilizing feature flags for Angular modules to enable lazy loading

Can we dynamically change the lazy loaded module based on a specific flag? For instance, loading module A if the flag is active and module B otherwise. The crucial aspect is that both modules should use the same path. Approach #1 - dynamic loadChildren() ...

Enhancing the tooltip inner content in Bootstrap with a description:

Can you help me troubleshoot the code below? I am trying to add a description to numbers in my tooltip, but it doesn't seem to be working. Here is the HTML: <input id="contract-length" class="slider slider-step-2 slider-contract-length" ...

Display a different string from an array at random, revealing each one gradually with a time delay

Looking for a way to display strings randomly without replacing one with another? I need a script that can shuffle words in a list and present them in a random order, with the ability to adjust the delay between each word. If you have a more effective code ...

Whenever I adjust the layout of the navigation bar, the edges end up getting clipped

I'm having trouble with the border shape of my navbar. When I try to make it a rounded pill shape, the edges get cut off instead of being properly displayed. https://i.stack.imgur.com/sUN2Y.png Below is the HTML template: <template> <div cl ...

What is the best way to ensure the height and width of a Next.js image matches the dimensions of the original image?

Currently, I am working on setting up an image gallery with a layout similar to Masonry. This layout involves multiple columns, all with the same width, adjusting based on the viewport width. The height of each item in the gallery is flexible, depending on ...

Clicking on the submit button in asp.net triggers the removal of the dropdown list items that were dynamically populated using a jQuery ajax request

I am struggling to populate a select box in asp.net using a jQuery ajax call. The value briefly appears when the submit button is clicked, but then disappears after the page refreshes. It seems to work fine with <input type="button">, but I am lookin ...

What is the best way to halt a for loop when the API response is not successful?

What's the best way to halt a for loop if the api response is false? Currently, my code integrates with an api by making calls within a loop. I'd like to continue calling the api only if the response is true. Here is the current implementation: ...