1

IMAP アカウントからメールを取得するための Zend Framework アプリケーションがあります。これにはしばらく時間がかかる場合があります。そのため、処理するメールの数をフロントエンドに表示したいと考えています。

私はこの解決策について考えています:

  • メールを処理するオブジェクトは、処理されたメールの量 (int) をファイル/データベースに書き込みます。
  • スクリプトの実行中に、このファイルの内容を 20 秒ごとに要求し、ユーザーに番号を表示します。

php/javascript を使用したこれに対するより良い解決策はありますか?

4

1 に答える 1

1

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>

編集: コード サンプルはW3Shamanからのものです

于 2012-08-28T15:55:03.703 に答える