两个ajax调用没有同时运行

我有两个使用jquery的ajax调用,第一个应该花费大约20秒来执行,但第二个要快得多,应该以毫秒为单位执行。 发生的事情是第二个ajax调用没有完成执行,直到第一个调用完成,一旦执行第一个调用,第二个ajax快速执行。 所以第二个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(); } } }); 

这是一场竞争。 你有这个:

do_download:将ajax调用设置为async get_operation_status:将ajax调用设置为sync

所以,你调用的第一个函数是“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); 

将两个调用async(永远不会永远不会SYNC)和文档中的计时器准备好:

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

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