Javascript &引用;“长轮询”;模拟占用整个网页 简要概述

Javascript &引用;“长轮询”;模拟占用整个网页 简要概述,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个基于回合的web应用程序,每个回合都有60秒的限制,除非他们在60秒之前结束回合,然后下一个用户的回合开始 它的工作原理是通过while语句防止在PHP页面上生成数据,该语句检查新数据,如果存在则生成,如果不存在则休眠并重置: while($olddata === $newdata){ sleep(2); /* get new data and repeat */ } 我从这些问题中得到了这个概念: 问题 但是,一旦调用开始,页面就会变得相对无响应;在超时完成或收到

我有一个基于回合的web应用程序,每个回合都有60秒的限制,除非他们在60秒之前结束回合,然后下一个用户的回合开始

它的工作原理是通过
while
语句防止在PHP页面上生成数据,该语句检查新数据,如果存在则生成,如果不存在则休眠并重置:

while($olddata === $newdata){
    sleep(2);
    /* get new data and repeat */
}
我从这些问题中得到了这个概念:

问题 但是,一旦调用开始,页面就会变得相对无响应;在超时完成或收到新数据之前,执行一些简单的操作(如刷新页面)将不起作用

如何配置此代码,使页面在等待新数据时保持响应

AJAX/jQuery代码 PHP代码
如果您使用
session\u start()
打开会话,最好在开始等待之前使用
session\u write\u close()
将其关闭,否则在所有其他请求中都会阻止对会话的访问

<?php

session_start();
require_once("../config.php");
require_once("classes.php");
require_once("functions.php");

$postedID = $_POST["currID"];
$draft = new Draft($user->getLeagueID());
$currID = $draft->getCurrDraftRow();

//This is the "long polling" simulation...
//the AJAX callback won't produce any information
//until the current ID is different to the ID
//on the page with the AJAX post function
while($currID == $postedID){
    session_write_close(); //added
    sleep(2);
    session_start(); //if needed, doesn't look like it is though
    $currID = $draft->getCurrDraftRow();
}

/* Get all the data - redacted because it's not important (and it works) */

//Output all the data as one JSON object
exit(json_encode(array($nid, $ndeadline, $nuserid, $fteam, $fdraft)));
session\u write\u close()很可能是您的问题。我会把它放在while循环之前,就像变量之后一样。另见此评论。
<?php

session_start();
require_once("../config.php");
require_once("classes.php");
require_once("functions.php");

$postedID = $_POST["currID"];
$draft = new Draft($user->getLeagueID());
$currID = $draft->getCurrDraftRow();

//This is the "long polling" simulation...
//the AJAX callback won't produce any information
//until the current ID is different to the ID
//on the page with the AJAX post function
while($currID == $postedID){
    sleep(2);
    $currID = $draft->getCurrDraftRow();
}

/* Get all the data - redacted because it's not important (and it works) */

//Output all the data as one JSON object
exit(json_encode(array($nid, $ndeadline, $nuserid, $fteam, $fdraft)));
<?php

session_start();
require_once("../config.php");
require_once("classes.php");
require_once("functions.php");

$postedID = $_POST["currID"];
$draft = new Draft($user->getLeagueID());
$currID = $draft->getCurrDraftRow();

//This is the "long polling" simulation...
//the AJAX callback won't produce any information
//until the current ID is different to the ID
//on the page with the AJAX post function
while($currID == $postedID){
    session_write_close(); //added
    sleep(2);
    session_start(); //if needed, doesn't look like it is though
    $currID = $draft->getCurrDraftRow();
}

/* Get all the data - redacted because it's not important (and it works) */

//Output all the data as one JSON object
exit(json_encode(array($nid, $ndeadline, $nuserid, $fteam, $fdraft)));