2

I need to create progressbar for long running PHP script so I decided to write script progress to json file and then read it by Javascript. Everything works fine in Firefox but if i change browser to Chrome it stops working. No errors in Console window. The Chrome simply stops loading json file when user starts PHP script but it does read this file before PHP execution start. Is it a problem with my code or normal behaviour of Chrome browser?

PHP:

$fp = fopen($this->writeFile, "w");
fwrite($fp, json_encode($values));
fclose($fp);

//for safety we will make a copy and let client access the copy.
copy($this->writeFile, $this->readFile);

Javascript:

getProgress();
function getProgress() {
    $.ajaxSetup({ cache: false });
    $.getJSON("../../../../upload/status.json", function(data) {
    var pbvalue = 0;
    if(data){
        var total = data['total'];
        var current = data['current'];
        if(total != 0)
            pbvalue = Math.floor((current / total) * 100);
        $(".progress-bar .indicator").width(pbvalue+"%");
        $(".progress-text").text(current+"/"+total);
    }
    if(pbvalue < 99)
        setTimeout("getProgress()", 3000);
});
}

JSON file:

{"total":"0","current":"0"}

EDIT 1: Thanks for timeout hint but it is working (good) in Firefox the problem is no "XHR finished loading" response inside Chrome console AFTER i start PHP execution (before it loads the file every 3 second as it is supposed to do)

EDIT 2: Probably the reason of this problem is that I try to read this JSON file AFTER user submits form. I guess that for some reasons Chrome after form submission no longer allow to execute jQuery $getJSON function. Any workarounds will be appreciated.

jmarceli
  • 19,102
  • 6
  • 69
  • 67
  • 3
    The PHP file could be timing out. Try: `set_time_limit(0);` – Samuel Cook Feb 04 '13 at 23:22
  • 2
    Just a side note, don't pass strings to `setTimeout`, it uses `eval`. Pass functions. `setTimeout(getProgress, 3000);` – gen_Eric Feb 04 '13 at 23:23
  • Chrome has issues: http://stackoverflow.com/questions/2541949/problems-with-jquery-getjson-using-local-files-in-chrome – Popnoodles Feb 04 '13 at 23:29
  • I followed your link tried --disable-web-security but it's not exactly my problem as in my case Javascipt in Chrome doesn't even try to get json file. It looks like it freezes during PHP execution. – jmarceli Feb 05 '13 at 00:01
  • So just to be clear: On FF everything works. On Chrome it only works before you run the PHP script. Is that correct? Also, what does it show under the Network tab? (is the request cancelled? is it blank?) – samitny Feb 06 '13 at 01:31
  • Yes on FF it works like it's supposed to. On Chrome it stops reading the file just after I submit the form and PHP script starts working. Network tab is a good hint I will check it. – jmarceli Feb 06 '13 at 01:59
  • In Network tab I see PENDING (status) next to my POST request to PHP script and no more requests for my JSON file. – jmarceli Feb 06 '13 at 02:07

1 Answers1

0

I've even tried making flash animation which reads json file but it didn't work either (looks like after submit Chrome stops updating currently displayed page). For me the only solution that works was redesigning code and executing PHP function by ajax call instead of traditional form submit.

jmarceli
  • 19,102
  • 6
  • 69
  • 67