weixin_33725270 2012-12-04 11:31 采纳率: 0%
浏览 33

完成ajax,然后重新开始

I'm working on a php-ajax chat, and I use two main methods: "update" and "send"

Update function (ajax):

$.ajax({
    type: 'POST',
    timeout: 9999999999999999,
    url: 'process.php',
    success: ...
});

Process.php:

function update(){
   if(new messages){
   echo new messages...
   }else{
   sleep(5);update(); again...
   }
}

Send function (ajax):

$.ajax({
type: "POST",
url: "send.php",
success: function(){
update();}
})

I want to stop running "update" request when sending new message and start it again immediently, so sender will instantly see his message. I have tried ajax.abort() but it can still continue to execute the call on server side.

How can I complete the ajax call and make return success, not aborting it?

  • 写回答

2条回答 默认 最新

  • weixin_33675507 2012-12-04 11:37
    关注

    In your PHP code you could check for connection aborted http://php.net/manual/en/function.connection-aborted.php

    But if you realy want to return "success" I would add something to the session in send.php like

    $_SESSION['SENDNOW']=true;
    

    and then in the Process.php I would check for

    if(new messages || $_SESSION['SENDNOW']==true){
       $_SESSION['SENDNOW']=false;
    }
    

    perhaps you should make the timeout 1sec for quicker response times. Or if you do it perfectly you could check quickly (like every 500ms) for $_SESSION['SENDNOW']==true and only every 5seconds for new messages

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿