使用:
- jQuery杀死Ajax请求
ignore_user_abort()
ob_flush()
应该在一个php线程中完成所有您需要的工作
编辑
看一下nickb的答案,如果您正在寻找一种简单地执行此操作的方法,它将遵循以下算法:
- javascript
process.php
通过ajax 打开(它将完成所有工作并打印状态报告),您必须查看jQuery ajax是否支持连续加载 - 如果用户决定停止刷新,则您将按提供的链接中所示终止加载
在
process.php:
ignore_user_abort(); // script will finish in backgroundwhile(...){ echo "Page: $in"; ob_flush();}EDIT 2 要求的示例 (不同而难看,但简单) 。
test_process.php:
// This script will write numbers from 1 to 100 into file (whatever happens)// And sends continuously info to user$fp = fopen( '/tmp/output.txt', 'w') or die('Failed to open');set_time_limit( 120);ignore_user_abort(true);for( $i = 0; $i < 100; $i++){ echo "<script type="text/javascript">parent.document.getElementById( 'foo').innerHTML += 'Line $i<br />';</script>"; echo str_repeat( ' ', 2048); flush(); ob_flush(); sleep(1); fwrite( $fp, "$in");}fclose( $fp);和主要的HTML页面:
<iframe id="loadarea"></iframe><br /><script>function helper() { document.getElementById('loadarea').src = 'test_process.php';}function kill() { document.getElementById('loadarea').src = '';}</script><input type="button" onclick="helper()" value="Start"><input type="button" onclick="kill()" value="Stop"><div id="foo"></div>在按如下开始线后:
Line 1Line 2
出现在div中
#foo。当我点击时
Stop,它们停止出现,但脚本在后台结束,并将所有100个数字写入文件中。
如果
Start再次点击,脚本将从乞讨(重写文件)开始执行,并行请求也会执行。
有关http流的更多信息,请参见此链接



