栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PHP-使用Ajax刷新While循环数据

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP-使用Ajax刷新While循环数据

使用:

  • jQuery杀死Ajax请求
  • ignore_user_abort()
  • ob_flush()

应该在一个php线程中完成所有您需要的工作

编辑

看一下nickb的答案,如果您正在寻找一种简单地执行此操作的方法,它将遵循以下算法:

  1. javascript
    process.php
    通过ajax 打开(它将完成所有工作并打印状态报告),您必须查看jQuery ajax是否支持连续加载
  2. 如果用户决定停止刷新,则您将按提供的链接中所示终止加载

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流的更多信息,请参见此链接



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/426334.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号