递归ajax()请求 [英] Recursive ajax() requests

查看:80
本文介绍了递归ajax()请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery的 ajax()来获取信息。我在请求成功时调用该方法。以下是代码:

I use jQuery's ajax()to get information. I call the method when the request is successful. Here is the code:

function recursively_ajax(){
    console.warn("begin");
    $.ajax({
        type:"GET",
        url: "./JvmInfoClass",
        success: function(data){
            console.warn("get jvm info success");
            recursively_ajax();
        }
    });
}

recursively_ajax();

我让线程在后端休眠3秒。但控制台不会在3秒后连续打印消息。这是为什么?

I make the thread sleep 3 seconds in the back-end. But the console print the message continuously not after 3 seconds. Why is this?

推荐答案

您可以尝试使用ajax调用async:false

You can try this with ajax call async:false

var counter=0;
 function recursively_ajax()
{
var pass_data=5;
var chartMenu=['VTR','NC','RC','TOCU','TOCO','TR','COA','MP'];
$.ajax({
        type:"POST",
        async:false, // set async false to wait for previous response
        url: "url to server",
        dataType:"json",
        data:{dataMenu:pass_data},
        success: function(data)
        {
            counter++;
            if(counter < chartMenu.length){
                recursively_ajax();
            }
        }
    });
 }      
 recursively_ajax();        

这篇关于递归ajax()请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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