Javascript 如何确定特定ajax调用的优先级?

Javascript 如何确定特定ajax调用的优先级?,javascript,ajax,Javascript,Ajax,我有两个基于ajax的方法,每10秒执行一次。但我还有一件事,那就是提交表格。现在,当我提交表单进行处理时,需要等待前面触发的ajax调用完成,然后再进行处理。我想把它列为优先事项 下面是我的脚本 function refresh_ss_one(){ $.ajax({ type: 'GET', url: "{{ route('RefreshSessionOne') }}", data: {}, success: function(

我有两个基于ajax的方法,每10秒执行一次。但我还有一件事,那就是提交表格。现在,当我提交表单进行处理时,需要等待前面触发的ajax调用完成,然后再进行处理。我想把它列为优先事项

下面是我的脚本

function refresh_ss_one(){
    $.ajax({
       type: 'GET',
       url: "{{ route('RefreshSessionOne') }}",
       data: {}, 
       success: function(response){ 
        console.log(response);
       }
    });
}
function refresh_ss_two(){
    $.ajax({
       type: 'GET',
       url: "{{ route('RefreshSessionTwo') }}",
       data: {}, 
       success: function(response){ 
        console.log(response);
       }
    });
}
setInterval(refresh_ss_one, 10000);
setInterval(refresh_ss_two, 10000);
我还有一个表单提交事件正在执行,我想对它进行优先级排序。我已经使用了
async
ajax参数,并在我的ajax函数中使用了这些参数,但仍然面临这个问题

$("#check-rate").submit(function(e) {
    var form_data = $(this);
    $.ajax({
           type: 'POST',
           url: currency_route,
           data: form_data.serialize(), 
           success: function(response)
           {
              ...
           }
         });
    e.preventDefault(); 
});

有人能告诉我如何解决这个问题吗?

您可以这样尝试:

<script>
// write your ajax function like
$.ajax({
    type: 'GET',
    url: "{{ route('RefreshSessionOne') }}",
    data: {},
    async: false,
    success: function(response) {
        console.log(response);
    }
});
//在完成上一个async:false请求后执行。

默认情况下,jquery中的$.ajax请求设置为异步。变量名为async,值设置为true


或者,您可以在第一个函数的成功部分调用第二个函数。

AJAX调用按它所说的做。它使请求异步。因此,根据定义,1请求不会等待其他请求完成。您可以将其设置为async:false,正如上面@jayoti所说的那样。但除此之外,你应该有一个方法调用另外两个方法,当你收到响应时,你可以调用第二个方法。

我会尝试一种更现代的方法。使用承诺:

var promise1 = new Promise(function(resolve, reject) {
   //first ajax call here
});

promise1.then((value) => {
//second ajax call here
});
这是它的理由

promise将确保运行代码的第一部分(这里是您的第一个ajax调用),您可以在ajax调用成功时设置resolve,然后它将运行第二个


另外,您可以拥有任意数量的请求。

要按顺序从get/POST请求列表中获取响应,您可以使用jQuery延迟来解决问题

const $ajaxSeq = arr => $.when.apply($, arr.map(data => $.ajax(data))).then(function(_res){ 
     return Array.isArray(_res) ? [].map.call(arguments, res => res[0]) : [_res];
  });
使用like:

$ajaxSeq([
    {type:'POST', url:'some_url_here', data: {content: "Hello world"}},
    {type:'GET',  url:'some_url_here'},
    {type:'GET',  url:'some_url_here'},
]).then( res => {
    // Once all requests are fulfilled
    // access the desired response in order:
    console.log(res[0])
    console.log(res[1])
    console.log(res[2])
});

这不应该发生……它不应该等待其他调用完成。如果我没有弄错,您希望按顺序执行ajax调用。为此,您可以在第一个调用的success函数中调用第二个调用,以此类推,或者按照承诺的方式进行。作为参考,ajax请求是独立的,不需要等待其他调用r请求完成,除非浏览器已达到可对同一主机同时发出请求的最大限制(最大限制因浏览器而异)
(async:false)
这是什么类型的JS语法?@RokoC.Buljan我刚刚添加了一个示例,您需要使用ajax请求的所有属性编写代码。而不是“示例”这样看起来更好:
({async:false,…})
$ajaxSeq([
    {type:'POST', url:'some_url_here', data: {content: "Hello world"}},
    {type:'GET',  url:'some_url_here'},
    {type:'GET',  url:'some_url_here'},
]).then( res => {
    // Once all requests are fulfilled
    // access the desired response in order:
    console.log(res[0])
    console.log(res[1])
    console.log(res[2])
});