【问题标题】:PHP responding to ajax when running a long script运行长脚本时 PHP 响应 ajax
【发布时间】:2014-10-22 15:39:13
【问题描述】:

我在here 提出了一个问题,回答并正常工作

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

事实上,我使用进度来复制文件,当我选择大文件时,经过 30% 的处理后,ajax 无法获得进度百分比。

如果你检查我的问题,你就会明白我在说什么。

复制.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); 

【问题讨论】:

  • 请在此处发布您的代码。
  • 好的,请稍候...
  • 在您的浏览器中,当 XHR 开始失败时返回的请求是什么?您的复制脚本是达到最大执行时间还是内存不足?你的 php 错误日志是怎么说的?
  • 没有错误,只是ajax挂了
  • 可能和stackoverflow.com/questions/25675304/…一样的问题,即需要使用session_write_close

标签: php jquery ajax copy progress-bar


【解决方案1】:

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

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

所以你的 javascript 会是这样的:

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

updateProgress();

【讨论】:

  • @Damon.s 控制台中有消息吗?
  • 任何不起作用的东西,例如因为 ajax 而喜欢其他帖子中的帖子或 cmets ...
  • @Damon.s 听起来像是某个地方的语法错误,请检查控制台。
  • 不。我确定这一点。代码运行几秒钟然后停止,复制完成后显示结果。
  • @Damon.s 你在使用会话吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-26
  • 1970-01-01
  • 2011-02-26
相关资源
最近更新 更多