'Ajax Call Request every 5 Seconds And Wait Before Sending The Request Until The Previous Request Is sent

I want to send ajax Request every 5 seconds And each request waits until the previous request is sent successfully , This is my code it works but it sends requests even if the previous request was not sent successfully.

Here My Code :

$(document).ready(function() {
  $('#myform').on('submit', function(e) {
    e.preventDefault();
    $("#sending").show();
    $("#send").attr('disabled', true);
    $("#send").attr('value', "Sending ...");

    var ajax_call = function() {

      $.ajax({
        url: "fbshr.php",
        type: "POST",
        data: {
          postURL: $('#postURL').val()
        },
        success: function(data) {
          $(data).appendTo($("#response")).before("");
          $('#response').animate({
            scrollTop: $('#response').prop("scrollHeight")
          }, 500);
          $("#num-msg-1").html($('#response p.alert-success').length);
          $("#num-msg-2").html($('#response p.alert-danger').length);

        }
      });
    };

    var interval = 5000;

    setInterval(ajax_call, interval);
  });
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source