代码之家  ›  专栏  ›  技术社区  ›  Deproblemify

页面上的PHP退出ajax中止()

  •  1
  • Deproblemify  · 技术社区  · 11 年前

    我想让浏览器等待任何通知,但如果单击链接,则停止等待。 这就是为什么我有一个PHP脚本 while 循环,如果发生了什么事情,该循环将结束,然后返回事件。

    PHP文件

    require 'required.php';
    
    ignore_user_abort ( false ); // I hoped this does it
    set_time_limit ( 60 );
    
    $lock = false; // maybe this helps?
    
    register_shutdown_function ( function () {
        // click the stop button in your browser ...
        $lock = true;
        exit ();
    } );
    
    if (isset ( $_SESSION ['user'] ))   {
    
        while ( ! new_event () && !$lock)   {
    
            if (connection_status () != CONNECTION_NORMAL) {
                break; // and maybe this works???
            }
            sleep(1);
        }
    
    echo json_encode (array('someparameter','...') );
    } else {
    echo 'login first';
    }
    

    我的JQueryAJAX代码在加载和调用后开始等待 abort() 单击链接后

    var pollReq;
    $(document).ready(function() {
            doPoll(); // do the first poll
    
    });
    
    function doPoll() {
       pollReq = $.post("wait.php", {parameter: value}, function(result) {
        // new event, do something
        doPoll();
        // this effectively causes the poll to run again as
        // soon as the response comes back
    }, 'json');
    }
    

    如果单击链接

    $('a[href]').not('noajax').click(
            function(e) {
                e.preventDefault();
                if (typeof pollReq !== 'undefined') {
                pollReq.abort(); // stop waiting
                }
       // Do something...
    });
    

    我找不到错误/原因,也许有人找到了。谢谢

    1 回复  |  直到 11 年前
        1
  •  2
  •   Community Mohan Dere    8 年前

    经过多次尝试和搜索,我终于发现,这都是关于会话的。幸亏 this 后来,我改变了PHP,让它在这段时间内可以正常工作 loop

    PHP文件

    while ( ! newMsg ( $chatID, $lastID ) ) {
        // Did the connection fail?
        if (connection_status () != CONNECTION_NORMAL) {
            break;
        }
        session_write_close();
        sleep(1);
        session_start();
    }
    

    如果你有更好的解决方案,我很乐意看到。

    编辑 : 这只会起作用,因为它允许另一个脚本在其间的1秒内获取会话锁定()。

    以及

    如果您不需要向会话写入任何内容: 放 session_write_close(); while 循环并省略 session_start();