views:

30

answers:

4

I've got a web page that allows to start a certain process and then redirects to another page that displays log file of that process. Since execution takes up to 10 minutes, I want log page to autoupdate itself or load data from the file periodically.

Right now I added

<meta http-equiv="refresh" content="5;url=log.php#bottom" /> 

to html/head but wondering if there may be a better solution. Can someone give any advice on aproaching this problem?

A: 

Use AJAX to do this. Easy in jQuery:

    <script type="text/javascript">

    $(function(){
        window.setInterval('updateLog()', 5000);
    });

    function updateLog() {
        $.get('log.php');
    }

    </script>
fire
A: 

Why not use javascript?

Use setInterval and run an AJAX call to log.php periodically.

You could also use an iframe, but the AJAX perdiodical call is a better way of doing it in my opinion.

robdog
+1  A: 

You could:

  • Periodically poll the server to see if there are more messages, basically you would call a PHP script with javascript and would pass the length of the log file in the last poll and then insert into the document the new data. The server would return all the data after that offset and also the new length.
  • (simpler) Make a long lived PHP script that keeps reading the file and echo and flush it as soon as there's new data. See PHP: How to read a file live that is constantly being written to.
Artefacto
+1  A: 

I do it this way:

var current_length = 0;
function update() {
  setTimeout(update, 3000);
  $.post("/update_url", { 'current_length': current_length }, function(data) {
    if (data.current_length != current_length) return; //it's too old answer
    $("#log").html($("#log").html() + data.text);
    current_length += data.text.length;
  }, "json"); 
}
update();

The server must skip several bytes at beginning and send json with current_length and the rest of file.

I prefer using memcached to store process output.

Riateche
Thanks, that worked. But I changed != to == at age check.
clorz