Delay GET until all POST methods have completed and not just started

I am having two problems with the jQuery record entry process, and I hope this wonderful SO community can help me solve at least one of these problems. These are the problems:

Issue 1 - Intermittent server latency

The first issue has to do with my Ubuntu 10.04 server showing intermittent 4.5 second delays when executing POST

data in mySQL database. Most commands POST

run in normal milliseconds, but when there is a delay, it always looks like 4.5 seconds. It is not a busy, public server, so the server load issue shouldn't be an issue. These short videos demonstrate what I am trying to explain:

Video 1 Video 2

I posted a question on serverfault and am awaiting some input from this forum which is probably more appropriate for this problem 1.

Issue 2 - Timing of JQuery POST and GET Methods

The real problem I am trying to resolve is to prevent the call GET

before all calls POST

have completed . Currently I have performed $.when.apply

to postpone the dispatch GET

. Here's the code for that:

function(){
  $.when.apply(undefined, InsertTheAPPs()).done(function (){
    $.ajax({
      url: sURL + "fileappeal/send_apps_email",
      success: function() {
        var m = $.msg("my message",
        {header:'my header', live:10000});
        setTimeout(function(){
          if(m)m.setBody('...my other message.');
        },3000);
        setTimeout(function(){
          if(m)m.close(function(){
              window.location.replace(sURL+'client/view');
          });
        },6000);
        $('#ajaxShield').fadeOut(1000);
      },
      error: function(){
          $.msg("error message",
          {header:'error header', live:10000});
      }
    });
  });
}

      

My problem is due to the delay described above in issue 1. The method GET

is called after all methods POST

have an initial one , but I need a method GET

until all methods POST

have completed . This is the problem I need help with. Basically what happens is what's going wrong is that my confirmation email is being sent before all the records have been fully inserted into the mySQL database.

Here is the code for the jQuery loop $.each

. This is the code that should not only begin , but should complete before the ajax call on fileappeal/send_apps_email

above:

function InsertTheAPPs(){
  $('input[name=c_maybe].c_box').each(function(){
    var jqxhrs = [];
    if($(this).prop('checked')){
      var rn = $(this).prop('value');
      jqxhrs.push(
        $.ajax({
          url: sURL + 'fileappeal/insert_app',
          type:"POST",
          dataType: 'text',
          data: {'rn': rn},
          error: function(data) {console.log('Error:'+rn+'_'+data);}
        })
      )
    return jqxhrs;
    }
  });
}

      

Does anyone have any suggestions on how I can get around the server latency issue and prevent the call GET

before all the methods have POST

completed? Thank.

+3


source to share


1 answer


There is a small problem with your post. Once you solve it, this post should help you finish the code: jQuery Deferred - pending multiple AJAX requests to complete

You are returning inside .each

, but the function itself does not return anything. This way your delay is not given an ajax awaits array. And also, since your jqhrs are defined inside each one, the scope is for each iteration on each c_box. Your method should look like this:

function InsertTheAPPs(){
    var jqxhrs = [];

    $('input[name=c_maybe].c_box').each(function(){         
        if($(this).prop('checked')){ 
            var rn = $(this).prop('value'); 
            jqxhrs.push(
                $.ajax({
                    url: sURL + 'fileappeal/insert_app',
                    type:"POST",
                    dataType: 'text',
                    data: {'rn': rn},
                    error: function(data) {console.log('Error:'+rn+'_'+data);}
                })
            )
        }
    });

    return jqxhrs;
} 

      



You can also make your code simpler. Since you just want to know if anything is checked, you can use a jquery pseudo-class filter :checked

like:

function InsertTheAPPs(){
    var jqxhrs = [];

    $('input[name=c_maybe].c_box').filter(':checked').each(function(){         
        var rn = $(this).prop('value'); 
        jqxhrs.push(
            $.ajax({
                url: sURL + 'fileappeal/insert_app',
                type:"POST",
                dataType: 'text',
                data: {'rn': rn},
                error: function(data) {console.log('Error:'+rn+'_'+data);}
            })
        )
    });

    return jqxhrs;
} 

      

You can combine a filter on :checked

into a main filter, for example $('input[name=c_maybe].c_box:checked')

, but I left it in long form to really demonstrate what is happening.

+1


source







All Articles