【问题标题】:real time progress bar implementation for phpphp的实时进度条实现
【发布时间】:2013-11-11 13:21:07
【问题描述】:

我有两页。我将 ajax 调用从第一页发送到第二页以在其中执行 php 代码。
执行需要很长时间(1-2 分钟)。如何创建进度条并在第一页显示进度百分比。
我可以计算进度百分比,但如何在第一页实时显示。

首页.php

$.ajax({ url: 'secondpage.php',
         data: {'q': data},
         type: 'post',
         success: function(output) {
                    //some code
            }

secondpage.php

    <?php

  // I have some codes here that can calculate progress percent
  // but how to show real time progress percent in firstpage.php

 echo $result;  //final result(for ajax callback)
    ?>

【问题讨论】:

    标签: php ajax progress-bar real-time


    【解决方案1】:

    flush() 和 ob_flush() 是你的朋友。将它们一起使用可以将输出发送到缓冲区。

    试试这个我从 php.net 获取的例子

    header( 'Content-type: text/html; charset=utf-8' );
    echo 'Begin ...<br />';
    for( $i = 0 ; $i < 10 ; $i++ )
    {
        echo $i . '<br />';
        flush();
        ob_flush();
        sleep(1);
    }
    echo 'End ...<br />';
    

    http://php.net/manual/en/function.ob-flush.php

    【讨论】:

    • Thanks.如何实时创建?
    【解决方案2】:

    我会使用 jquery ui 进度条。 http://jqueryui.com/progressbar/

    我会使用 ajax 调用来获取已完成的百分比,并使用已完成的值在同一页面上更新进度条。 您也许可以每 10 秒左右调用一次进度,获取更新,并相应地更新进度条。

    【讨论】:

    • 谢谢我测试你的答案
    猜你喜欢
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2012-08-18
    相关资源
    最近更新 更多