为什么“等待”?我的Ajax通话时长度如此之长? (Chrome网络面板) [英] Why is the "waiting" length so long on my Ajax call? (Chrome Network Panel)

查看:70
本文介绍了为什么“等待”?我的Ajax通话时长度如此之长? (Chrome网络面板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在请求一些json内容的页面上有几个ajax调用。在所有这些电话中,我在响应完成时获得了大量的等待时间。对于这些呼叫中的每一个,呼叫都会有几秒钟的等待时间,如下面的Chrome网络面板中所示。我附上了一张图片:

I have a few ajax calls on a page that request some json content. On all these calls, I am getting a significant wait time on the response completing. For each of these calls, there is a "waiting" period of a few seconds in the call as shown in Chrome Network panel below. I have attached a picture:

我不确定是什么导致这种情况,因为我对查询数据库的php代码做了一些基准测试,据此,查询和处理json发送回的调用在0.001秒左右运行。

I am not really sure what is causing that, as I did some benchmarking on the php code that is querying the database, and according to that, the call for the query and processing the json to send back is running in 0.001 seconds or so.

那么,这只是网络延迟吗?这是一个我没有正确进行数据库查询的问题吗?也许我充斥着每个浏览器窗口的最大连接数?不知道。其他请求移动速度一样慢,所以看起来它可能是一致的。

So, is this just a network latency thing? Is this a problem where I am not doing the database query correctly? Maybe I'm flooding the maximum connections per browser window? No idea. The other requests are moving just as slowly, so it seems like it could be something consistent.

这是其余请求时间的另一张照片(与另一个请求ajax调用与get_usergames_simple调用的时间一样多:

Here is another photo of the rest of the requests timing (with the other main ajax call taking just as much time as the get_usergames_simple call:

作为参考,这里是ajax调用:

For reference, here is the ajax call:

self.getGamesContent = function()
{
  var userID = "<?php echo $userID; ?>";

  var post_data = {
    userID: userID
  };

  $.post("https://mydomain.com/games/get_usergames_simple/", post_data, function(result)
  {
    var json = $.parseJSON(result);

    var mappedGames = $.map(json.games, function(item) {
      return new GameItem(item)
    });
    self.gameitems(mappedGames);
  });
};

这里是运行查询的控制器中的php代码:

And here is the php code in the controller running the query:

$userID = $this->input->post('userID');
$this->benchmark->mark('code_start');

$userGames = $this->cache->model('games', 'getGamesSimpleByUserID', array($userID), 120); // keep for 2 minutes

$returnString = "{";

$returnString .= '"user_id": "' . $userID . '",';

$gameCount = 0;

$returnString .= '"games": [';
foreach ($userGames as $ug)
{
  $returnString .= "{";
  $returnString .= '"user_id" : "' . $userID . '",';
  $returnString .= '"game_id" : "' . $ug->GameID . '",';
  $returnString .= '"game_name" : "' . $ug->GameName . '",';
  $returnString .= '"game_image" : "' . $ug->GameImage . '",';
  $returnString .= '"description" : "' . htmlspecialchars($ug->GameDescription) . '",';
  $returnString .= '"genre_id" : "' . $ug->GameGenreCatID . '",';
  $returnString .= '"genre_name" : "' . $ug->GameGenreName . '",';
  $returnString .= '"publisher_id" : "' . $ug->GamePublisherID . '",';
  $returnString .= '"publisher_name" : "' . $ug->GamePublisherName . '",';
  $returnString .= '"developer_id" : "' . $ug->GameDeveloperID . '",';
  $returnString .= '"developer_name" : "' . $ug->GameDeveloperName . '",';
  $returnString .= '"active_flag" : "' . $ug->GameIsActive . '",';
  $returnString .= '"create_date" : "' . $ug->GameCreateDate . '",';
  $returnString .= '"remove_date" : "' . $ug->GameRemoveDate . '",';
  $returnString .= '"last_update_date" : "' . $ug->GameLastUpdateDate . '",';
  $returnString .= '"user_syncing_game" : "' . $ug->UserSyncingGame . '"';
  $returnString .= "},";
  $gameCount++;
}

if ($gameCount > 0)
  $returnString = substr($returnString, 0, strlen($returnString) - 1);

$returnString .= "]}";


$this->benchmark->mark('code_end');

//echo $this->benchmark->elapsed_time('code_start', 'code_end');

echo $returnString;


推荐答案

肯定控制器的构造函数中有一些缓慢的动作。

Definitely there are some slow actions in the controller's constructor.

在Codeigniter中使用内置探查器要好得多:

It's much better to use built-in profiler in Codeigniter:

http://ellislab.com/codeigniter/user-guide/general/profiling.html

这篇关于为什么“等待”?我的Ajax通话时长度如此之长? (Chrome网络面板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆