多个并行AJAX需要较长的服务器响应时间

时间:2015-06-14 20:26:05

标签: javascript php jquery ajax

我的.js文件中同时有五个ajax。它们需要很长的服务器响应时间(最短5秒)。如果我逐个调用它们,那么它们每个都会在1秒内执行。

这是我的js代码。

$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);    
      });
 }

如果我执行所有ajax,请查看image1中的图像,然后完成所有请求需要4-5秒。在image2中,我只执行了一个ajax,然后在一秒钟内完成了自己。

image1

image2

并且我还没有在我的服务器端脚本中使用任何会话。处理请求和创建响应只是简单的PHP脚本。

我已经google搜索如何同时使用多个ajax并找到以下链接 -

Two simultaneous AJAX requests won't run in parallel

因为它在服务器端脚本中使用会话,所以它使用了session_write_close(),但是我没有在我的服务器端脚本中使用任何会话,但我仍然尝试在发送响应之前放置此函数,但没有工作

任何人都可以告诉我要同时执行所有ajax请求我需要做什么。?

如果有任何不清楚的地方请询问。

0 个答案:

没有答案