Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJAX响应,用于循环难度_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript AJAX响应,用于循环难度

Javascript AJAX响应,用于循环难度,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有个小问题。也就是说,我想得到我的变量的值:count_green在for循环之外。有人知道我怎么做吗? 以下是我的代码片段: function State(type, count, list_name){ var count_green = 0; var count_yellow = 0; var count_red = 0; for (i = 0; i < count; i++){ var jqxhr = $.ajax( "/custom_data/getState/" +

我有个小问题。也就是说,我想得到我的变量的值:count_green在for循环之外。有人知道我怎么做吗? 以下是我的代码片段:

function State(type, count, list_name){
var count_green = 0;
var count_yellow = 0;
var count_red = 0;

for (i = 0; i < count; i++){
    var jqxhr = $.ajax( "/custom_data/getState/" + type[i]).done(function(result) {
        for (j = 0; j < count; j++){
            if(result.type == type[j]){
                if(result.value == "GREEN"){
                    $("#" + type[j]).css("color","#468847");
                    count_green = count_green + 1;
                }
                if(result.value == "YELLOW"){
                    $("#" + type[j]).css("color","#f89406");
                    count_yellow = count_yellow + 1; 
                }
                if(result.value == "RED"){
                    $("#" + type[j]).css("color","#b94a48");
                    count_red = count_red + 1;
                }
            }
        }
    });
}
    //Here I would need the value of count_green, count_yellow and count_green
    //Unfortunately they are outside of the for loop 0.
    addCounters(count_green, count_yellow, count_red);
}

function addCounters(state_green, state_yellow, state_red){
    $(".line").find(".list").append('<span class="badge pull-left count_badge badge-success">' + state_green + '</span>');
}
功能状态(类型、计数、列表名称){
var count_green=0;
var count_yellow=0;
风险值计数=0;
对于(i=0;i
如您所见,如果我将方法调用addCounters放入for循环中,我会得到许多徽章,说明为什么我需要访问count变量:count_green、count_yellow和count_red,它们位于for循环和ajax代码之外。 我看到了一些使用回调的解决方案,但我只需要变量值的一倍,如果我使用回调,我得到的值将超过1倍


提前感谢您。

一个解决方案是同步请求。看看async属性

$.ajax( "/custom_data/getState/" + type[i], {async: false}).done(...)
另一个解决方案是这样的

var requestCount = 0;

for (i = 0; i < count; i++){
    var jqxhr = $.ajax( "/custom_data/getState/" + type[i]).done(function(result) {
        for (j = 0; j < count; j++){
            if(result.type == type[j]){
                if(result.value == "GREEN"){
                    $("#" + type[j]).css("color","#468847");
                    count_green = count_green + 1;
                }
                if(result.value == "YELLOW"){
                    $("#" + type[j]).css("color","#f89406");
                    count_yellow = count_yellow + 1;
                }
                if(result.value == "RED"){
                    $("#" + type[j]).css("color","#b94a48");
                    count_red = count_red + 1;
                }
            }
        }
        requestFinished(count_green, count_yellow, count_red);
    });
}

function requestFinished(count_green, count_yellow, count_red){
    requestCount++;
    if(requestCount == count){//all requests finished
       addCounters(count_green, count_yellow, count_red);
    }
}
var requestCount=0;
对于(i=0;i
嗨,michael,谢谢您的回复。还有其他解决办法吗?