Nick
Nick

Reputation: 1102

Ajax request waiting time

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?

Upvotes: 0

Views: 1481

Answers (1)

Lawrence Cherone
Lawrence Cherone

Reputation: 46650

If you are using PDO for the connection.

Instead of using localhost as the host use 127.0.0.1

Upvotes: 2

Related Questions