-1

I've asked a question at here that answered and working fine,

but my problem is that when script running about more than 4~5 seconds , all ajax requests not working at all until completing running script .

In fact I've using a progress to copy files and when I choose a large file , after 30% process the ajax not working to get percentage of progress .

If you check my question , you will understand what I've saying .

Copy.php

$f = mysql_query(" select * from `process` ");
$f = mysql_fetch_assoc($f);
$total_bytes_readed = $f['total-bytes-readed'];
$total_bytes = $f['total-bytes'];
$abort = $f['state'] == 'abort'?true:false;
$remote = fopen($src, 'r');
$local = fopen($des, 'w');
$filesize = filesize($src);
$d = strtotime(date('Y-m-d H:i:s'));
$B = $speed = $time_remaining = $read_bytes = 0;

while(!feof($remote) && !$abort) {

    $field = mysql_query(" select * from `process` ");
    $field = mysql_fetch_assoc($field);
    if($field['state']=='abort')
    {
        fclose($remote);
        fclose($local);
        if(file_exists($des))
        unlink($des);
        $abort = true;
        return;
    }else{
        $bitrate = 2048*2048;
        $buffer = fread($remote, $bitrate);
        fwrite($local, $buffer);
        $read_bytes += $bitrate ;
        $total_bytes_readed += $bitrate;
        $D = strtotime(date('Y-m-d H:i:s'));        
        if($D-$d >=1)
        {
            $d = $D;
            $speed = $read_bytes - $B;
            $B = $read_bytes;
            $total_bytes_remaining = $total_bytes - $total_bytes_readed;
            $time_remaining = $total_bytes_remaining/$speed;
        }
        mysql_query("UPDATE `process` SET 
                    `total-bytes-readed` = '$total_bytes_readed',
                    `speed` = '$speed',
                    `time-remaining` = '$time_remaining' 
                     WHERE `process`.`id` =1");         
    }
}
fclose($remote);
fclose($local);
mysql_query("UPDATE `process` SET 
            `total-bytes-readed` = '$total_bytes_readed',
            `speed` = '$speed',
            `time-remaining` = '$time_remaining' 
             WHERE `process`.`id` =1");

progress.php

if(isset($_POST['cancel']) && $_POST['cancel']==1)
mysql_query("UPDATE `process` SET `state` = 'abort' WHERE `process`.`id` =1");  

$f = mysql_query(" select * from `process` ");
$f = mysql_fetch_assoc($f);

$re = array('totalfiles'  => $f['total-files'],
            'totalbytes'  => $f['total-bytes'],
            'bytesreaded' => $f['total-bytes-readed'],
            'file'        => $f['file'],
            'from'        => $f['from'],
            'to'          => $f['to'],
            'speed'       => ByteToSize($f['speed'])."/Second",
            'time'        => seconds_to_time($f['time-remaining']),
            'items'       => $f['items-remaining'],
            'state'       => $f['state'],
            );
echo json_encode($re);

JS

setInterval(function(){
    $.post( progress.php,{cancel:cancel_val},
    function(data){
       //...        
    }); 
},300); 
7
  • 1
    Please post your code here. Commented Oct 22, 2014 at 15:42
  • Okay , please wait ... Commented Oct 22, 2014 at 15:43
  • in your browser what are the XHR requests returning when it starts to fail? is your copy script hitting it's max execution time, or running out of memory? what do your php error logs say? Commented Oct 22, 2014 at 15:57
  • there is no error , just ajax hanging Commented Oct 22, 2014 at 16:01
  • 1
    Possibly the same problem as stackoverflow.com/questions/25675304/…, i.e you need to use session_write_close Commented Oct 22, 2014 at 16:02

1 Answer 1

0

Without seeing it in action, I would guess that the problem is that you have multiple ajax requests that are overlapping.

Instead of using an setInterval, you should set use a setTimeout in the success function of your ajax call. That way you ensure that there is always only 1 ajax request at most at the same time.

So your javascript would be something like:

function updateProgress() {
    $.post( progress.php,{cancel:cancel_val},
    function(data){
       //... 
       setTimeout(updateProgress, 300);      
    }); 
};

updateProgress();
Sign up to request clarification or add additional context in comments.

5 Comments

@Damon.s Any messages in the console?
anythings not working , for example liking posts or comments in other posts because of ajax ...
@Damon.s Sounds like a syntax error somewhere, check the console.
nope . I'm sure about this . code running for a few seconds then stop and when copying has been completed the result shown .
@Damon.s Are you using sessions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.