乱世@小熊 2012-10-13 14:14 采纳率: 25%
浏览 126

Ajax请求等待时间

I've been working on a project that required me to use Ajax. In the past I have had no problems creating a project with it, but this time every request takes at least 1 second, which is ofcourse way too long.

I can't give you the entire code, but I'll share as much as possible. The requests are as follows:

/* *
 * Update the navigation screen
 */
    function UpdateNavigation() {
         $.ajax({
            type: "POST",
            url: "application/controllers/LocationController.php",
            dataType: "json",
            data: "action=GetSurroundings",
            success: function(data){
                $("#direction-north").html((typeof data.north != 'undefined' ? data.north : "") + '</br> North');
                $("#direction-west").html((typeof data.west != 'undefined' ? data.west : "") + '</br> West');
                $("#direction-center").html((typeof data.center != 'undefined' ? data.center : "") + '</br> Center');
                $("#direction-east").html((typeof data.east != 'undefined' ? data.east : "") + '</br> East');
                $("#direction-south").html((typeof data.south != 'undefined' ? data.south : "") + '</br> South');
            }
        });
    }

/* *
 * Update the current location
 */
    $('#navigation-list :button').click(function(event) {
        if (event.target.id == "direction-center")
            return;

         $.ajax({
            type: "POST",
            url: "application/controllers/LocationController.php",
            data: "action=SetLocation&value=" + event.target.id,
            success: function() {
                UpdateNavigation();
            }
        });
    });

As far as I know this could is fine. It might be a good idea to change it a little but it shouldn't cause the delay as far as I know. The LocationController file is pretty big, but it uses a switch for different cases so the actual executed code isn't that big.

Using FireBug I found out that it's waiting for 1.01 seconds for the first request, and 1.00 second for the second request. I did some research and people told me that this might be because the server is too busy to handle your request properly so that's why it's taking so long. But that seems unlikely because the code and database are hosted locally. There shouldn't be a conflict in requests either since they are executed one at a time.

I'm at a loss here. I have no idea how to start debugging this problem. Deleting parts of the code didn't help because it would either stop executing alltogether or just take 1+ second. This leads me to believe the code is not the problem, although I could be wrong.

Any help would be greatly apreciated! If you need more information, please don't hesitate to ask.

Edit: Some more digging around leads me to believe the queries inside the code may be at fault? If so, I'm using the following (pretty ugly) queries:

SELECT 
    character_location.block,
    character_location.location
FROM
    character_location
WHERE
    character_location.id = 1
LIMIT
    1



SELECT 
    zones.name,
    zones.location,
    zones.block
FROM
    zones
WHERE
    (
            `zones`.`location` = (".$getBlock['location']." - 1)
        AND
            zones.block = '".$playerBlock."'
    )
OR
    (
            `zones`.`location` = ".$getBlock['location']."
        AND
            zones.block = '".$playerBlock."'
    )
OR
    (
            `zones`.`location` = (".$getBlock['location']." + 1)
        AND
            zones.block = '".$playerBlock."'
    )
OR
    (
            `zones`.`location` = ".$getBlock['location']."
        AND
            zones.block = '".$playerBlockDown."'
    )
OR
    (
            `zones`.`location` = ".$getBlock['location']."
        AND
            zones.block = '".$playerBlockUp."'
    )
LIMIT
    5


UPDATE 
    character_location
SET
    character_location.block = '" . $targetBlock . "',
    `character_location`.`location` = " . $targetLocation . "
WHERE
    character_location.id = 1

Guess which one I think might be causing the problem?

  • 写回答

1条回答 默认 最新

  • ?yb? 2012-10-13 14:54
    关注

    If you are using PDO for the connection.

    Instead of using localhost as the host use 127.0.0.1

    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝