我在请求一些 json 内容的页面上有几个 ajax 调用。在所有这些电话中,我在响应完成时等待了很长时间。对于这些呼叫中的每一个,呼叫中有几秒钟的“等待”时间,如下面的 Chrome 网络面板所示。我附上了一张图片:
我不确定是什么原因造成的,因为我对查询数据库的 php 代码进行了一些基准测试,据此,查询和处理 json 以发送回的调用在 0.001 秒左右运行。
那么,这只是网络延迟的事情吗?这是我没有正确执行数据库查询的问题吗?也许我正在淹没每个浏览器窗口的最大连接数?不知道。其他请求的移动速度也一样慢,所以看起来它可能是一致的。
这是其余请求时间的另一张照片(其他主要 ajax 调用所用的时间与 get_usergames_simple 调用一样多:
作为参考,这里是 ajax 调用:
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 代码:
$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;