Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting for all AJAX call than show dialog box

I am working on a dynamic online form website. In the main form, I have multiple sub-forms which can be added and deleted dynamically.

<div class='subform'>
    //form fields 
    <input ...>
    ...
    <button class='subform_submit'>
</div>

For each subform, I bind an AJAX call on the subform's submit button like this:

$('#main').on('click', '.subform_submit', function(){
    // Get this subform's user input
    ...
    $.ajax({
        url: ..,
        type: ..,
        data: /* this subform's data */
    });
});

So in that page, I may have 0 to 10 subforms depending on the user's selection. I also have a main submit button on the bottom of the page, which can submit those subforms and the main form's data together.

$('#main').on('click', '#submit', function(e){
    $('.subform_submit').click(); // Submit each subform
    bootbox.confirm({ });
})

Once main submit button is clicked, I want to show a loading picture and then show a dialog box (I use bootbox.confirm() here) until all AJAX calls have completed. This dialog box is telling user that whole form including sub-forms has been submitted. But the problem is that each AJAX call may take 2 seconds to complete and I don't know how may calls may be pending completion. How can I write this main submit button so that it will:

  1. Show the loading image immediately, and
  2. Hide the loading image and show the dialog box after all AJAX calls have completed?
like image 293
monknom Avatar asked Dec 22 '15 15:12

monknom


People also ask

How do you check if all AJAX calls are completed?

jQuery ajaxStop() Method The ajaxStop() method specifies a function to run when ALL AJAX requests have completed. When an AJAX request completes, jQuery checks if there are any more AJAX requests. The function specified with the ajaxStop() method will run if no other requests are pending.

How do I make jQuery wait for an AJAX call to finish before it returns?

If you don't want the $. ajax() function to return immediately, set the async option to false : $(". my_link").

Is there a limit to AJAX calls?

The AJAX Toolkit uses SOAP API calls. SOAP calls always have a maximum limit of 50MB. However, it is also XML-based, which restricts the available characters you can use, so the file has to be Base-64 encoded. This puts the final limit at around 37 MB of binary data, as you've observed.

How wait for AJAX call in selenium?

Moreover, the JavaScript Executor can be used to wait for an Ajax call. The executeScript method is used to run a JavaScript command in Selenium. The waiting is done till jQuery. active command yields 0.


1 Answers

Keep track of how many sub-forms there are;

$subFormsCount = $('.subform').length;

Keep track of how many forms have been submitted;

$submittedForms = 0;

Each time a form finishes submitting, add to the $submittedForms;

$.ajax({
  ..
  ..
  done: function(){
    $submittedForms++;
  }
})

Create a global timer to see if the number of submitted forms matches the total number of subforms. If true, hide the dialog box;

setInterval(function(){
  if($submittedForms == $subFormsCount){
   $('.dialog').show();
  }
}, 50ms)

Edit

You could skip the global timer (as this will probably be a few milliseconds out) - include the check in your ajax.done instead;

$.ajax({
  ..
  ..
  done: function(){
    $submittedForms++;

    if($submittedForms == $subFormsCount){
     $('.dialog').show();
    }
  }
})
like image 60
Lewis Avatar answered Sep 29 '22 14:09

Lewis