PHP在运行长脚本时响应ajax


PHP responding to ajax when running a long script

我在这里问了一个问题,回答了,工作正常

但我的问题是,当脚本运行超过4~5秒时,所有ajax请求在完成脚本运行之前根本不起作用。

事实上,我使用进度来复制文件,当我选择一个大文件时,经过30%的处理,ajax不起作用,以获得进度的百分比。

如果你核对一下我的问题,你就会明白我在说什么。

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); 

如果没有看到它的实际操作,我猜问题在于您有多个重叠的ajax请求。

您应该在ajax调用的成功函数中设置使用setTimeout,而不是使用setInterval。这样可以确保在同一时间最多只有一个ajax请求。

因此,您的javascript将类似于:

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