jan*_*on0 5 ajax jquery codeigniter knockout.js
我在一个请求一些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);
});
};
Run Code Online (Sandbox Code Playgroud)
这是运行查询的控制器中的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;
Run Code Online (Sandbox Code Playgroud)
控制器的构造函数中肯定有一些缓慢的动作。
最好使用 Codeigniter 中的内置分析器:
http://ellislab.com/codeigniter/user-guide/general/profiling.html