How do I close a connection early?

前端 未结 19 1434
情书的邮戳
情书的邮戳 2020-11-22 04:24

I\'m attempting to do an AJAX call (via JQuery) that will initiate a fairly long process. I\'d like the script to simply send a response indicating that the process has star

19条回答
  •  -上瘾入骨i
    2020-11-22 04:55

    Ok, so basically the way jQuery does the XHR request, even the ob_flush method will not work because you are unable to run a function on each onreadystatechange. jQuery checks the state, then chooses the proper actions to take (complete,error,success,timeout). And although I was unable to find a reference, I recall hearing that this does not work with all XHR implementations. A method that I believe should work for you is a cross between the ob_flush and forever-frame polling.

    {$str}";
     };
    
     ob_start(); // begin buffering output
     echo wrap("console.log('test1');");
     ob_flush(); // push current buffer
     flush(); // this flush actually pushed to the browser
     $t = time();
     while($t > (time() - 3)) {} // wait 3 seconds
     echo wrap("console.log('test2');");
    ?>
    
    
     
      
     
    
    

    And because the scripts are executed inline, as the buffers are flushed, you get execution. To make this useful, change the console.log to a callback method defined in you main script setup to receive data and act on it. Hope this helps. Cheers, Morgan.

提交回复
热议问题