duancong2965 2013-08-29 16:37
浏览 57
已采纳

Ajax + php长轮询得到了错误

I´m trying to do the ajax php polling function it seemed to work fine but I can´t understand what´s wrong or did I misunderstood something ?

Let´s see my code:

*In 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'] ?>);">

In 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

 }
}

In 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);

It won´t work until I change to ($r['dtimestamp'] > $cur_time) ; *I wonder why ?*

Because the logic is $cur_time( which post from javascript is the original timestamp in index.php. and $r['dtimestamp'] is the curent timstamp in database. This is the opposite from logic.

  • 写回答

1条回答 默认 最新

  • douliaodan2738 2013-08-29 16:47
    关注

    Because until $r['dtimestamp'] > $cur_time you have told it to sleep 10 and do clearstatcache(),

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

    it is only once $r['dtimestamp'] > $cur_time that it gets out of your while loop and if $r['dtimestamp'] < $cur_time to start with it will never suddenly get > unless something in clearstatcache is changing $cur_time

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败