PHP-JQuery实现进程长时间运行请求

PHP-JQuery实现进程长时间运行请求,php,zend-framework,Php,Zend Framework,我有一个Zend Framework应用程序,可以从IMAP帐户获取邮件。这可能需要一段时间。所以我想在前端显示还有多少邮件需要处理 我正在考虑这个解决方案: 处理邮件的对象正在将处理的邮件量(int)写入文件/db 脚本运行时,每20秒请求一次此文件的内容,并向用户显示数字 有没有更好的php/javascript解决方案 您可以使用flush()在某些点向浏览器输出数据。诚然,这不是一个很好的解决方案,但您可以(应该)重构: <!DOCTYPE HTML PUBLIC "-//W3

我有一个Zend Framework应用程序,可以从IMAP帐户获取邮件。这可能需要一段时间。所以我想在前端显示还有多少邮件需要处理

我正在考虑这个解决方案:

  • 处理邮件的对象正在将处理的邮件量(int)写入文件/db
  • 脚本运行时,每20秒请求一次此文件的内容,并向用户显示数字
有没有更好的php/javascript解决方案

您可以使用flush()在某些点向浏览器输出数据。诚然,这不是一个很好的解决方案,但您可以(应该)重构:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>Progress Bar</title>
</head>
<body>
<!-- Progress bar holder -->
<div id="progress" style="width:500px;border:1px solid #ccc;"></div>
<!-- Progress information -->
<div id="information" style="width"></div>
<?php
// Total processes
$total = 10;

// Loop through process
for($i=1; $i<=$total; $i++){
    // Calculate the percentation
    $percent = intval($i/$total * 100)."%";

    // Javascript for updating the progress bar and information
    echo '<script language="javascript">
    document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\">&nbsp;</div>";
    document.getElementById("information").innerHTML="'.$i.' row(s) processed.";
    </script>';

    // This is for the buffer achieve the minimum size in order to flush data
    echo str_repeat(' ',1024*64);

    // Send output to browser immediately
    flush();

    // Sleep one second so we can see the delay
    sleep(1);
}

// Tell user that the process is completed
echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"</script>';
?>
</body>
</html>

进度条
编辑:代码示例来自

您可以使用flush()在某些点向浏览器输出数据。诚然,这不是一个很好的解决方案,但您可以(应该)重构:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>Progress Bar</title>
</head>
<body>
<!-- Progress bar holder -->
<div id="progress" style="width:500px;border:1px solid #ccc;"></div>
<!-- Progress information -->
<div id="information" style="width"></div>
<?php
// Total processes
$total = 10;

// Loop through process
for($i=1; $i<=$total; $i++){
    // Calculate the percentation
    $percent = intval($i/$total * 100)."%";

    // Javascript for updating the progress bar and information
    echo '<script language="javascript">
    document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\">&nbsp;</div>";
    document.getElementById("information").innerHTML="'.$i.' row(s) processed.";
    </script>';

    // This is for the buffer achieve the minimum size in order to flush data
    echo str_repeat(' ',1024*64);

    // Send output to browser immediately
    flush();

    // Sleep one second so we can see the delay
    sleep(1);
}

// Tell user that the process is completed
echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"</script>';
?>
</body>
</html>

进度条
编辑:代码示例来自

dosleep()是错误的,因为它会降低速度而没有任何好处。只要不要在循环的每次迭代中
echo()
,就完成了。如果调用
flush()
@WebnetMobile.com Sleep()只是为了演示,那么填充缓冲区也是毫无意义的。显然,如果你是为了一个特定的进程而这样做的话,你会把它去掉是错误的,因为它会降低速度而没有任何好处。只要不要在循环的每次迭代中
echo()
,就完成了。如果调用
flush()
@WebnetMobile.com Sleep()只是为了演示,那么填充缓冲区也是毫无意义的。显然,如果你是为了一个特定的过程而做的,你会把它去掉。