AJAX请求导致超时

时间:2017-03-03 23:40:22

标签: javascript php jquery mysql ajax

我对AJAX请求不是很熟练,但我的标题中有这个简单的脚本:

    function refresh_div() {
    jQuery.ajax({
        url:'progress.php?game=<?php echo $gamename; ?>',
        type:'POST',
        success:function(results) {
            jQuery(".middle").html(results);
        }
    });
}

    t = setInterval(refresh_div,1000);

然后在progress.php文件中我有来自我的db的很​​多mysql请求,目的是每秒用这个php数据刷新页面。这种情况大多数都可以正常工作,但我发现无论何时我几次同时调用此脚本几次,我都会遇到超时错误并且页面不会加载。这一切都适用于基于浏览器的游戏,其中包括一个每秒倒计时的计时器,所以重要的是我发现每秒都能稳定执行php请求的东西。

添加了progress.php:

<?php



session_start(); 
$username = $_SESSION["username"];


$gamename = $_GET['game'];



require "../connections/games.php";

$newgamesql = "SELECT * FROM gamelist WHERE name = '$gamename'";
$newgamequery = mysql_query($newgamesql, $gamesdbhandle);

$newgamecount = mysql_num_rows($newgamequery);

$gamenamenospaces = str_replace(' ','_', $gamename);

$gamestatus = mysql_result($newgamequery, 0, 'status'); 

$numberofplayers = mysql_result($newgamequery, 0, 'numberofplayers'); 


$player1 = mysql_result($newgamequery, 0, 'player1');
$player2 = mysql_result($newgamequery, 0, 'player2');
$player3 = mysql_result($newgamequery, 0, 'player3');
$player4 = mysql_result($newgamequery, 0, 'player4');
$player5 = mysql_result($newgamequery, 0, 'player5');
$player6 = mysql_result($newgamequery, 0, 'player6');
$player7 = mysql_result($newgamequery, 0, 'player7');
$player8 = mysql_result($newgamequery, 0, 'player8');

$rounds = mysql_result($newgamequery, 0, 'rounds'); 

$currentround = mysql_result($newgamequery, 0, 'currentround'); 

$gametimestamp = mysql_result($newgamequery, 0, 'actualtime'); 

$headmaster = mysql_result($newgamequery, 0, 'headmaster'); 

$roundtime = mysql_result($newgamequery, 0, 'time'); 

$activeplayers = mysql_result($newgamequery, 0, 'activeplayers'); 

$numberofplayers = mysql_result($newgamequery, 0, 'numberofplayers'); 

$roundtimeproper = strtotime("H:i:s" , $roundtime);

//Round time conversion goes here: 

//The specific table query for the individual rounds


$thegamesql = "SELECT * FROM `$gamenamenospaces` WHERE id='$currentround' LIMIT 1";
$thegamequery = mysql_query($thegamesql, $gamesdbhandle);


$echocurrentround = mysql_num_rows($thegamequery) + 1;

$thisroundplayer1 = mysql_result($thegamequery, 0, "player1card");
$thisroundplayer2 = mysql_result($thegamequery, 0, "player2card");
$thisroundplayer3 = mysql_result($thegamequery, 0, "player3card");
$thisroundplayer4 = mysql_result($thegamequery, 0, "player4card");
$thisroundplayer5 = mysql_result($thegamequery, 0, "player5card");
$thisroundplayer6 = mysql_result($thegamequery, 0, "player6card");
$thisroundplayer7 = mysql_result($thegamequery, 0, "player7card");
$thisroundplayer8 = mysql_result($thegamequery, 0, "player8card");

$thisroundwhitecard = mysql_result($thegamequery, 0, "whitecard");
$thisroundwhitecard = str_replace("_", "_____", $thisroundwhitecard);


$thisroundtime = mysql_result($thegamequery, 0, "timestamp");



$now = new DateTime();
$target = DateTime::createFromFormat('H:i:s', $gametimestamp);
$difference = $now->diff($target);

$difference = $difference->format("%I:%S");


//Display the timer

echo '<div id="timer">';
echo 'TIME:';
echo '<br />';
echo $difference; 
echo '</div>';
//echo $timer; 



//State which round

echo '<div id="toptitle">';
echo $gamename; 
echo ' - ';
echo 'Round ';
echo $currentround;
echo '</div>';

if ($gamestatus == 'active') {
//Game is live, display things

echo '<div id="placeholderplaceholder">';


//The card submit bar

if (!empty($player1)) { 

if (!empty($player1card)) {

echo '<div class="blackcardplaceholderplaced">';

}else{

echo '<div class="blackcardplaceholder">';

}

echo 'P1';


echo '</div>';

}

if (!empty($player2)) { 

if (!empty($player2card)) {

echo '<div class="blackcardplaceholderplaced">';

}else{

echo '<div class="blackcardplaceholder">';  

}

echo 'P2';


echo '</div>';

}

if (!empty($player3)) {

if (!empty($player3card)) {

echo '<div class="blackcardplaceholderplaced">';

}else{ 

echo '<div class="blackcardplaceholder">';  

}

echo 'P3';


echo '</div>';

}

if (!empty($player4)) { 

if (!empty($player4card)) {

echo '<div class="blackcardplaceholderplaced">';

}else{

echo '<div class="blackcardplaceholder">';  

}

echo 'P4';


echo '</div>';

}

if (!empty($player5)) { 

if (!empty($player5card)) {

echo '<div class="blackcardplaceholderplaced">';

}else{

echo '<div class="blackcardplaceholder">';  

}

echo 'P5';


echo '</div>';

}

if (!empty($player6)) { 

if (!empty($player6card)) {

echo '<div class="blackcardplaceholderplaced">';

}else{

echo '<div class="blackcardplaceholder">';  

}

echo 'P6';


echo '</div>';

}

if (!empty($player7)) { 

if (!empty($player7card)) {

echo '<div class="blackcardplaceholderplaced">';

}else{

echo '<div class="blackcardplaceholder">';  

}

echo 'P7';


echo '</div>';

}

if (!empty($player8)) { 

if (!empty($player8card)) {

echo '<div class="blackcardplaceholderplaced">';

}else{

echo '<div class="blackcardplaceholder">';  

}

echo 'P8';


echo '</div>';

}

echo '</div>';



if ($headmaster == $username) {
//You are the headmaster, show the judge view

echo '<br />Your turn to <div id="headmaster">Judge</div><br /><br />';

if(!empty($thisroundwhitecard)) {
echo '<div class="bigwhitecard">';
echo $thisroundwhitecard; 
echo '</div>';
}


}else{
//You are not the headmaster, show the player view  

//Randomly generate 5 cards

require '../dbauth/cards.inc.php';
    $cardsdbhandle = mysql_connect(

    $cardshostname, 
    $cardsusername, 
    $cardspassword

    ) or die("INVALID USERNAME OR PASSWORD");

    $cardsselected = mysql_select_db($cardsdbname, $cardsdbhandle);

$newblackcardsql = "SELECT * FROM black LIMIT 5";
$newblackcardquery = mysql_query($newblackcardsql, $cardsdbhandle);

$newblackcardcount = mysql_num_rows($newblackcardquery);

$c = 0; 

if ($newblackcardcount > 0) {

    echo '<div class="blackcardscroller">';
    echo '<div class="blackcardscrollerinner">';

while ($c < $newblackcardcount) {

$blackcardid = mysql_result($newblackcardquery, $c, "id");
$blackcardtext = mysql_result($newblackcardquery, $c, "text");


echo '<div class="gameblackcard">';
echo $blackcardtext;

echo '<div class="blackcardbtn" id="';
echo $blackcardid; 
echo '">PICK CARD</div>';   
echo '</div>';

$c++; 

}

echo '</div>';
echo '</div>';

}else{
//There are no black cards

echo 'There are no black cards to pick from! Something went wrong...';

}

echo '<br />Judge: <br />';
echo '<div id="headmaster">';
echo $headmaster;
echo '</div>';





if(!empty($thisroundwhitecard)) {
echo '<div class="bigwhitecard">';
echo $thisroundwhitecard; 
echo '</div>';
}



}

}



?>

2 个答案:

答案 0 :(得分:1)

您不应该使用setInterval ...请改为:

function refresh_div() {
    jQuery.ajax({
        url:'progress.php?game=<?php echo $gamename; ?>',
        type:'POST',
        success:function(results) {
            jQuery(".middle").html(results);
            setTimeout(function(){
                refresh_div();
            }, 1000);
        }
    });
}

refresh_div();

答案 1 :(得分:0)

我觉得你在这个字符串中有问题

t = setInterval(refresh_div,1000);

您每秒都会从服务器获得查询,因此在10秒后,行中有10个查询,这可能会导致超时,因为服务器太忙而无法回答,也可能是因为并发查询很多其他问题。所以我建议你尝试使用promise(),看一下这篇文章:

in JavaScript, how to wrap a promise in timeout?