在长轮询功能中中止AJAX请求

时间:2017-02-24 08:02:29

标签: php jquery ajax bioinformatics long-polling

我正在实施一个能够监控后台工作的应用程序(BLAST program)。

我希望当PID等于0时,然后停止轮询过程。

但在我的长轮询示例中,即使使用abort()函数,轮询过程也会继续运行。

此解决方案对我不起作用。

var poll_xhr;

    (function doPoll() {
        setTimeout(function() {
            poll_xhr = $.ajax({
                type: 'GET',
                url: 'longpoll.php',
                error: function (request, textStatus, errorThrown) {
                    displayError();
                },
                success: function(result) {
                    if (result == 0) {

                        // To kill the AJAX request (Not Working)
                        console.log("The process is not running.");
                        poll_xhr.abort();
                    }
                    else 
                        console.log(result);        
                },
                dataType: "json",
                complete: doPoll,
                timeout: 5000
            });
        })
    })();   

PHP代码:

    $pid = $_SESSION['PID'];

    if(file_exists('/proc/'. $pid)){
        // $pid is running
        echo json_encode($pid);
    }
    else{ 
        $e = 0;
        echo json_encode($e); 
    }

如何取消投票过程?任何建议将不胜感激。

0 个答案:

没有答案
相关问题