2 Ajax调用不会同时运行 [英] two ajax calls are not running simultaneously

查看:85
本文介绍了2 Ajax调用不会同时运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery 2 Ajax调用,第一个应该花时间大约20秒内执行,但第二个要快得多,应该以毫秒为单位执行。正在发生的事情是第二个Ajax调用没有完成执行,直到第一个是完整的,一旦第一个调用执行,第二Ajax是执行速度快。因此第二AJAX正在被呼叫与第一个,但它并没有完成执行,直到第一个是完整的。我该如何解决呢?

下面是我的code:

  jQuery的(文件)。就绪(函数(五){
        do_download();

    });
    功能do_download()
    {
        $('。状态P')文本(抓取视频......');
        VAR请求= $阿贾克斯({
            网址:http://www.example.com/download/start
            方法:POST,
            异步:真正的,
            数据:{youtube_url:https://www.youtube.com/watch?v=ic7scBTY-xw,
                    access_token:4b5e0c903eb7b68eb336500cdfc2d11c
                }
            });

            request.done(函数(MSG){
                //警报('完成');


            });

            request.fail(功能(jqXHR,textStatus){
                警报(请求失败:+ textStatus);
            });
    }


    VAR get_operation_status_interval = setInterval的(get_operation_status,1000);

    功能get_operation_status(){
        VAR url_process_info =htt​​p://www.example.com/download/get_video_status_new/ic7scBTY-xw;
       $阿贾克斯({
            网址:url_process_info,
            数据类型:JSON,
            异步:假的,
            方法:GET,
            缓存:假的,
            成功:功能(数据){
                如果(data.progress){
                    $('div.meter跨度)的CSS('宽',data.progress);
                    如果(data.progress =='100%'和;&安培;!data.is_file_ready = FALSE){
                        $('。状态P')文本(转换视频......');
                    }
                }
                如果(data.is_file_ready ==真){
                    clearInterval(get_operation_status_interval);
                    $('。状态P')文本('文件准备好了!');
                    $('。下载节)显示()。
                }
            }
        });
 

解决方案

您:第二个是异步:假的。将其更改为true。(或者完全删除它)

I have two ajax calls using jquery, The first one should take time around 20 seconds to be executed, but the second one is much faster, should be executed in Milliseconds. What is happening is the second ajax call is not finishing executing until the first one is complete, once the first call is executed, the second ajax is executed fast. So the second ajax is being called with the first one but it doesn't finish executing until the first one is complete. How can I fix that ?

Here is my code:

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

解决方案

Your 2nd one is async: false. Change it to true.(or remove it completely)

这篇关于2 Ajax调用不会同时运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆