【问题标题】:two ajax calls are not running simultaneously两个 ajax 调用没有同时运行
【发布时间】:2015-07-03 23:07:00
【问题描述】:

我有两个使用 jquery 的 ajax 调用,第一个应该需要大约 20 秒才能执行,但第二个要快得多,应该以毫秒为单位执行。发生的事情是第二个 ajax 调用直到第一个调用完成才完成执行,一旦执行第一个调用,第二个 ajax 就会快速执行。因此,第二个 ajax 与第一个一起被调用,但在第一个完成之前它不会完成执行。我该如何解决?

这是我的代码:

jQuery(document).ready(function (e) {
        do_download();

    });
    function do_download()
    {
        $('.status p').text('Fetching the video ...');
        var request = $.ajax({
            url: "http://www.example.com/download/start",
            method: "POST",
            async: true,
            data: {youtube_url: 'https://www.youtube.com/watch?v=ic7scBTY-xw',
                    access_token :'4b5e0c903eb7b68eb336500cdfc2d11c'
                }
            });

            request.done(function (msg) {
                //alert('done');


            });

            request.fail(function (jqXHR, textStatus) {
                alert("Request failed: " + textStatus);
            });
    }


    var get_operation_status_interval = setInterval(get_operation_status, 1000);

    function get_operation_status() {
        var url_process_info = "http://www.example.com/download/get_video_status_new/ic7scBTY-xw";
       $.ajax({
            url: url_process_info,
            dataType: 'json',
            async: false,
            method: "GET",
            cache: false,
            success: function(data) {
                if(data.progress){
                    $('div.meter span').css('width', data.progress);
                    if( data.progress == '100%' && data.is_file_ready != false){
                        $('.status p').text('Converting the video ...');
                    }
                }
                if (data.is_file_ready == true) {
                    clearInterval(get_operation_status_interval);
                    $('.status p').text('file is ready !');
                    $('.download-section').show();
                }
            }
        });

【问题讨论】:

    标签: javascript jquery ajax


    【解决方案1】:

    这是一个竞争条件。你有这个:

    do_download:将 ajax 调用设置为异步 get_operation_status:将 ajax 调用设置为同步

    所以,你调用的第一个函数是“get_operation_status”,为什么?因为 do_download 会在文档准备好时调用,而另一个函数会在之前调用,所以浏览器需要时间来准备文档,然后第一个调用是“get_operation_status”,这个函数会冻结其他调用,因为它是同步而不是异步:

    // tames some time, it's no inmediately
    jQuery(document).ready(function (e) {
        do_download();
    });
    
    // this call could be execute before do_download
    var get_operation_status_interval = setInterval(get_operation_status, 1000);
    

    将两个调用异步(从不从不同步)和文档内的计时器准备为:

    jQuery(document).ready(function (e) {
        do_download();
        var get_operation_status_interval = setInterval(get_operation_status, 1000);
    });
    

    【讨论】:

    • 我已经将函数的设置间隔移动到您提到的准备好的文档中,但仍然无法正常工作,这是来自 firebug screencast.com/t/fYdn7W5b的快照
    • 这是我将两个调用设置为 async = true 时的快照,第二个 ajax 已被多次调用,但在 do_download 完成之前没有一个被执行screencast.com/t/qnxwmVVGov
    • 好的,一个问题,您是否在代码中使用了一些警报?请尝试删除所有“警报”功能,因为它是一个同步功能并阻止主 javascript 线程。
    • 问题似乎与codeigniter有关,我被问到了here的问题
    【解决方案2】:

    你的第二个是 async: false。将其更改为 true。(或将其完全删除)

    【讨论】:

    • 我将第二个调用设置为 async: false,它会多次调用它,但直到第一次调用完成之前,都没有完成调用
    猜你喜欢
    • 1970-01-01
    • 2013-07-27
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多