Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery AJAX triggering too quickly
    primarykey
    data
    text
    <p>I have a relatively simple jQuery AJAX call wrapped in a function and I am testing my error functionality. The problem I am facing is the AJAX call happens too quickly! It is causing my 'H6' and '.loading' elements to start repeating. The behaviour I require is to remove the elements, then call the ajax.</p> <pre><code>function getAvailability(form) { var str = $(form).serialize(), warning = $('#content h6'); if ( warning.length &gt; 0 ) { $(warning).remove(); $('&lt;div class="loading"&gt;Loading&amp;hellip;&lt;/div&gt;').insertAfter(form); } else { $('&lt;div class="loading"&gt;Loading&amp;hellip;&lt;/div&gt;').insertAfter(form); } $.ajax({ type: "POST", url: "someFile", data: str, success: function(calendar) { $('.loading').fadeOut(function() { $(this).remove(); $(calendar).insertAfter(form).hide().fadeIn(); }); }, error: function() { $('.loading').fadeOut(function() { $('&lt;h6&gt;Unfortunately there has been an error and we can not show you the availability at this time.&lt;/h6&gt;').insertAfter(form); }); } }); return false; } </code></pre> <p>I would love to sequence it like so -> Remove 'warning' from page, add .loading. Then trigger AJAX. Then fade out .loading, add &amp; fade in warning/calendar dependent on success.</p> <hr> <p>I have amended my original code, and I have got the function to behave as expected, primarily because I have disabled the submit button during the ajax process.</p> <pre><code>function getAvailability(form) { var str = $(form).serialize(), btn = $('#property_availability'); // Disable submit btn, remove original 'warning', add loading spinner btn.attr("disabled", "true"); $('.warning').remove(); $('&lt;div class="loading"&gt;Loading&amp;hellip;&lt;/div&gt;').insertAfter(form); $.ajax({ type: "POST", url: "public/ajax/returnAvailability1.php", data: str, success: function(calendar) { $('.loading').fadeOut(function() { $(this).remove(); $(calendar).insertAfter(form).hide().fadeIn(); }); }, error: function() { $('.loading').fadeOut(function() { $(this).remove(); $('&lt;h6 class="warning"&gt;Unfortunately there has been an error and we can not show you the availability at this time.&lt;/h6&gt;').insertAfter(form); btn.removeAttr("disabled"); }); } }); return false; } </code></pre> <p>I believe that the original sequence was not working as expected due to the time delay created by the fadeOut() functions.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload