1

I have five ajax simultaneously in my .js file. They are taking very long server response time(minimum 5sec). If I call them one by one then each of them executes within 1 sec.

here is my js code.

$j('.combo').change(function(){
        filter_area();
        filter_subarea();
        filter_propertytype();
        filter_pricerange();
        filter_bedrooms();
});

function filter_area()
 {
      var area_id = $j('#inputarea').val();
      var operation = $j('#property').val();
      var subarea_id = $j("#inputsubarea").val();
      var propertytype = $j("#inputpropertytype").val();
      var pricerange = $j("#inputpricerange").val();
      var bedrooms = $j("#inputbedrooms").val();

     $j.ajax({
          type:"GET",
          url:'/custom_action.php',
           data:'action=filter_area'+'&operation='+operation+'&area_id='+area_id+'&subarea_id='+subarea_id+'&propertytype='+propertytype+'&pricerange='+pricerange+'&bedrooms='+bedrooms,
      }).done(function(response){         
          $j('#inputarea').html(response);    
      });
 }


function filter_subarea()
 {
      var area_id = $j('#inputarea').val();
      var operation = $j('#property').val();
      var subarea_id = $j("#inputsubarea").val();
      var propertytype = $j("#inputpropertytype").val();
      var pricerange = $j("#inputpricerange").val();
      var bedrooms = $j("#inputbedrooms").val();

     $j.ajax({
          type:"GET",
          url:'/custom_action.php',
           data:'action=filter_area'+'&operation='+operation+'&area_id='+area_id+'&subarea_id='+subarea_id+'&propertytype='+propertytype+'&pricerange='+pricerange+'&bedrooms='+bedrooms,
      }).done(function(response){         
          $j('#inputsubarea').html(response);     
      });
 }


function filter_propertytype()
 {
      var area_id = $j('#inputarea').val();
      var operation = $j('#property').val();
      var subarea_id = $j("#inputsubarea").val();
      var propertytype = $j("#inputpropertytype").val();
      var pricerange = $j("#inputpricerange").val();
      var bedrooms = $j("#inputbedrooms").val();

     $j.ajax({
          type:"GET",
          url:'/custom_action.php',
           data:'action=filter_area'+'&operation='+operation+'&area_id='+area_id+'&subarea_id='+subarea_id+'&propertytype='+propertytype+'&pricerange='+pricerange+'&bedrooms='+bedrooms,
      }).done(function(response){         
          $j('#inputpropertytype').html(response);    
      });
 }


function filter_pricerange()
 {
      var area_id = $j('#inputarea').val();
      var operation = $j('#property').val();
      var subarea_id = $j("#inputsubarea").val();
      var propertytype = $j("#inputpropertytype").val();
      var pricerange = $j("#inputpricerange").val();
      var bedrooms = $j("#inputbedrooms").val();

     $j.ajax({
          type:"GET",
          url:'/custom_action.php',
           data:'action=filter_area'+'&operation='+operation+'&area_id='+area_id+'&subarea_id='+subarea_id+'&propertytype='+propertytype+'&pricerange='+pricerange+'&bedrooms='+bedrooms,
      }).done(function(response){         
          $j('#inputpricerange').html(response);      
      });
 }

function filter_bedrooms()
 {
      var area_id = $j('#inputarea').val();
      var operation = $j('#property').val();
      var subarea_id = $j("#inputsubarea").val();
      var propertytype = $j("#inputpropertytype").val();
      var pricerange = $j("#inputpricerange").val();
      var bedrooms = $j("#inputbedrooms").val();

     $j.ajax({
          type:"GET",
          url:'/custom_action.php',
           data:'action=filter_area'+'&operation='+operation+'&area_id='+area_id+'&subarea_id='+subarea_id+'&propertytype='+propertytype+'&pricerange='+pricerange+'&bedrooms='+bedrooms,
      }).done(function(response){         
          $j('#inputbedrooms').html(response);    
      });
 }

Please see images in image1 if I execute all ajax then it takes 4-5 sec to complete all requests. and in image2 I have executed only one ajax out of them then it finished itself within one sec.

image1

image2

and also I have not used any session at my server side script. It is just simple PHP script to handle request and create response.

I have googled to find how to use multiple ajax simultaneously and found the below link-

Two simultaneous AJAX requests won't run in parallel

Since this uses session in server side script, so it has used session_write_close(), but I have no used of any session in my server side script, still I tried to put this function before sending my response, but nothing worked.

Can any one please tell me what I have to do to execute all ajax request concurrently.?

If any thing is not clear please ask.

Community
  • 1
  • 1
shashank
  • 566
  • 3
  • 10
  • 31
  • 1
    Firstly, you should seriously refactorize your code – A. Wolff Jun 14 '15 at 20:27
  • Hey@Wolff, did not get your means by refactorize. can you please explain it more. Am I doing something wrong in my js code.? – shashank Jun 14 '15 at 20:30
  • See DRY principle: http://geekswithblogs.net/chrisfalter/archive/2008/03/07/refactor-to-dry.aspx All your methods are doing the same thing except for a little line. BTW, you'd have better to just send only one ajax request and handle all data to return server side. You could return JSON from server – A. Wolff Jun 14 '15 at 20:33
  • I take it the `action` parameter should change, `action=filter_area`, `action=filter_subarea` etc. – Roamer-1888 Jun 14 '15 at 21:51

0 Answers0