加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

为什么我的Ajax调用中的“等待”长度如此之长? (Chrome网络面板

发布时间:2020-12-15 22:50:59 所属栏目:百科 来源:网络整理
导读:我在一个请求一些json内容的页面上有几个ajax调用.在所有这些电话中,我在响应完成时获得了大量的等待时间.对于这些呼叫中的每一个,呼叫都会有几秒钟的“等待”时间,如下面的“Chrome网络”面板中所示.我附上了一张照片: 我不确定是什么导致了这一点,因为我
我在一个请求一些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,strlen($returnString) - 1);

$returnString .= "]}";


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

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

echo $returnString;

解决方法

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

在Codeigniter中使用内置的分析器要好得多:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读