0

ajax php ポーリング機能を実行しようとしていますが、正常に動作しているように見えましたが、何が問題なのか理解できませんでしたか、それとも何か誤解していましたか?

私のコードを見てみましょう:

* index.php 内*

  $tst = $conn->prepare('SELECT dtimestamp FROM inbx where recipient=:recipient  ORDER BY dtimestamp DESC LIMIT 1');

  $tst->bindParam(':recipient', $row['user_id']);
  $tst->execute();
  $rk= $tst->fetch();

<body onload="chkinbx(<?php echo $row['user_id']?>, <?php echo $rk['dtimestamp'] ?>);">

JavaScriptで

function chkinbx (user_id, cur_time) {

var old_timestamp=0;

var url="function.php?user_id="+user_id+"&cur_time="+cur_time;
var params="user_id="+encodeURIComponent(user_id)+"&cur_time="+encodeURIComponent(cur_time);


xmlHttp=GetXmlHttpObject()
xmlHttp.onreadystatechange=function () {

 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){

var result=xmlHttp.responseText;
var res=JSON.parse(result);


if(res[2]==old_timestamp || cur_time==res[2]){

////  do nothing

}else{
    document.getElementById(bb).innerHTML="<a class='topNav' href='inbox.php'><span class='countmsg'>"+res[1]+"</span></a>";


}

setTimeout(function() {
chkinbx (res[3], res[4], cur_time);  // div   user_id   curtime

}, 1000); //8 seconds

 }
}

function.php で

$params=$_POST['params'];
$user_id=$_POST['user_id']; 
$cur_time=$_POST['cur_time'];

  $stmt = $conn->prepare('SELECT dtimestamp FROM inbx where recipient=:recipient AND inbxicn_is_click=:inbxicn_is_click  ORDER BY database_timestamp DESC' );
  $stmt->execute(array(':recipient'=>$user_id,':inbxicn_is_click'=>'N'));
  $r=$stmt->fetch();

while ($r['dtimestamp'] <= $cur_time) {
  sleep(10);
  clearstatcache();

 }

     $phpArray = array("ok",$stmt->rowCount(), $r['database_timestamp'], $user_id);
     echo json_encode($phpArray);

($r['dtimestamp'] > $cur_time) に変更するまで機能しません。 *なぜだろう?*

ロジックは $cur_time( javascript からの投稿が index.php の元のタイムスタンプであり、$r['dtimestamp'] がデータベースの現在のタイムスタンプであるためです。これはロジックとは逆です。

4

1 に答える 1

0

$r['dtimestamp'] > $cur_time までは、10 をスリープして clearstatcache() を実行するように指示しているため、

while ($r['dtimestamp'] <= $cur_time) {
  sleep(10);
  clearstatcache();

 }

$r['dtimestamp'] > $cur_time が while ループから抜け出すのは 1 回だけであり、$r['dtimestamp'] < $cur_time で開始する場合は、clearstatcache 内の何かが変更されていない限り、突然取得されることはありません $ cur_time

于 2013-08-29T16:47:58.963 に答える