weixin_33701564 2017-07-10 06:10 采纳率: 0%
浏览 44

Ajax调用不并行

i have two ajax calls. the first call is for read a file and save it into the DB (mysql) and at the bottom of a for loop they set an session variable of the "status" like the interval. The second call return the session variable.

this is my javascript code:

 var interval = null;
function test(data) {
    var i = 0;
    interval = setInterval(function () {
        $.ajax({
            url: '/admin/movies/progress',
            type: "GET",
            async: true,
            dataType: "text",
            success: function (data) {
                console.log(data);
                $('#saveFileProgressBar').width(data[0]);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                toastr.error('error progressbar', 'Download File');
            }
        });
        i++;
        if(i == 5){
            clearInterval(interval);
        }
    }, 500);

    $.ajax({
        url: '/admin/movies/1',
        type: "GET",
        async: true,
        dataType: "text",
        success: function (data) {
            console.log(data);

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            toastr.error('error', 'Download File');
        }
    });
}

this is my laravel 5.4 code:

// Mapped to mysite.com/admin/movies/progress
public function getProgress() {   
    return Response::json(array(Session::get('progress')));
}

// Mapped to mysite.com/admin/movies/1
public function postGenerate() {
    // getting values from form (like $record_num)
    Session::put('progress', 0);
    Session::save(); // Remember to call save()

    for ($i = 1; $i < 100; $i++) {
        sleep(1);
        Session::put('progress', $i);
        Session::save(); // Remember to call save()
    }

    return "done";
}

enter image description here

///////EDIT///////

my new PHP code:

public function getProgress() {
    $rawData = file_get_contents('plugins/elFinder-2.1.25/files/data/progressFile.json');
    $cacheData = json_decode($rawData, true);
    return $cacheData;
}

public function postGenerate() {
    // getting values from form (like $record_num)
    for ($i = 0; $i < 10; $i++) {
        $data['progress'] = $i;
        $fres = fopen('plugins/elFinder-2.1.25/files/data/progressFile.json', 'w');
        fwrite($fres, json_encode($data));
        fclose($fres);
        sleep(1);
    }
    return "true";
}

my javascript code:

 var interval = null;
function test() {
    var i = 0;
    interval = setInterval(function () {
        $.ajax({
            url: '/admin/movies/progress',
            type: "GET",
            async: true,
            dataType: "text",
            success: function (data) {
                console.log(data);
                $('#saveFileProgressBar').width(data.progress);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                toastr.error('Es ist ein Fehler aufgetreten beim Auslesen der Datei', 'Download File');
            }
        });
        i++;
        if(i == 5){
            clearInterval(interval);
        }
    }, 500);

    $.ajax({
        url: '/admin/movies/1',
        type: "GET",
        async: true,
        dataType: "text",
        success: function (data) {
            console.log(data);

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            toastr.error('Es ist ein Fehler aufgetreten beim Auslesen der Datei', 'Download File');
        }
    });
}
  • 写回答

2条回答 默认 最新

  • DragonWar% 2017-07-13 07:29
    关注

    Instead of using session use regular file. Session files are locked for each unique session until the script is finished. so It cant be ran parallel. What you can do is store the data in plain file and access it.

    To read out from the cached file

     $rawData = file_get_contents('data/progressFile.json');
     $cacheData = json_decode($rawData, true);
    

    write like

    $data['progress'] = 100;
            $fres = fopen('data/progressFile.json', 'w');
            fwrite($fres, json_encode($data));
            fclose($fres)
    

    You can use json data structure for all the session and parse to get particular progress data or you may use separate file for ech progress data. if you are using one large file you may need to use flock() to lock the file during the writing.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。