2012-04-26 131 views
2

我正在阅读很多关于彗星编程的事情,但我在想这个实现是否可能。PHP输出缓冲循环和jQuery的Ajax

这是我的JScript

$.ajax({ 
    url:"mypage.php", 
    onProgress:{function(rsp){ //i know there is no such function like this 
    $("#mydiv").append(rsp+"<br>"); 
    }} 
}); 

,这是我mypage.php

<?php 
    while(true){ 
    ob_start(); 
     echo time(); 
    ob_flush(); 
    } 
    ?> 

现在我要的是我的AJAX请求将获取进程中的结果。这可能吗?

+0

佩塔提克瓦的在http://stackoverflow.com/questions/7740646/jquery-ajax-read-the-stream-incrementally该解决方案的答案工作的请客我 – 2016-08-08 13:44:37

回答

1

这种事情发生,你必须使用彗星技术.... 请参阅

​​3210

+1

甚至感谢我使用PHP,我可以理解的代码在ASP.NET中 – 2012-04-26 06:51:49

1

继承人的投票方式待办事项它,如果你唯一的希望抢时间。为什么有一个无限循环使用资源。

<?php 
//polling.php 
if(isset($_GET['poll']) && $_GET['poll']=='1'){ 
$out = array('thetime'=>date("F j, Y, g:i:s a")); 
echo json_encode($out); 
die; 
} 
?> 


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script> 
function poll(){ 
    setTimeout(function(){ 

     $.ajax({ url: "http://localhost/polling.php?poll=1",cache: false, 
     success: function(data){ 
     //Do something with returned json 
     $("#time").replaceWith("<p id=\"time\">The Server Time & Date is: "+ data.thetime +"</p>"); 
     //Next poll 
     poll(); 
     }, dataType: "json"}); 
    // 1/2 a sec 
    }, 500); 
} 

$(document).ready(function(){ 
    poll(); 
}); 
</script> 

<p id="time">The Server Time & Date is: <?php echo date("F j, Y, g:i:s a");?></p>