jQuery ajax中的循环问题

时间:2022-08-26 11:15:11

This js loop script always get the last value of ui_item inside a jquery ajax funciton. How can a catch the correct value of each iteration?

这个js循环脚本总是在jquery ajax函数中获取ui_item的最后一个值。如何捕获每个迭代的正确值?

for (var i = 0; i <= split_files_cb_value_holder.length - 1; i++){
    var split_values = split_files_cb_value_holder[i].split(':');

    ui_item = split_files_cb_value_holder[i];

    $.ajax({
        type: "POST",
        url: "ds/index.php/playlist/check_folder",
        data: "component_type="+$('#component_type').val()+"&value="+split_values[1],
        success: function(msg)
        {
            console.log(ui_item); //ALWAYS GETS THE LAST VALUE
        },
        error: function()
        {
            alert("An error occured while updating. Try again in a while");
        }
    });

}

Thanks!

谢谢!

2 个解决方案

#1


45  

The problem is that the anonymous callback method captures the ui_item variable by reference. Since there is only one variable, it always gets whatever was assigned last to the variable.

问题是匿名回调方法通过引用捕获ui_item变量。因为只有一个变量,它总是得到最后分配给变量的东西。

You need to wrap the contents of the for loop in a function that takes i as a parameter, then call the function in the loop. Each call to the wrapper function will create a separate variable, solving the problem.

您需要将for循环的内容封装到一个以i为参数的函数中,然后在循环中调用该函数。每个对包装器函数的调用都将创建一个单独的变量,从而解决问题。

For example:

例如:

function doCheck(i) {
    var split_values = split_files_cb_value_holder[i].split(':');

    var ui_item = split_files_cb_value_holder[i];

    $.ajax({
        type: "POST",
        url: "ds/index.php/playlist/check_folder",
        data: "component_type="+$('#component_type').val()+"&value="+split_values[1],
        success: function(msg)
        {
            console.log(ui_item); //Don't always get the last value
        },
        error: function()
        {
            alert("An error occured while updating. Try again in a while");
        }
    });
}

for (var i = 0; i < split_files_cb_value_holder.length; i++) 
    doCheck(i);

#2


-5  

Turn async off, it will fix the problem i guess. I mean add this: async:false

关闭异步,它将解决我猜想的问题。我的意思是添加这个:async:false

#1


45  

The problem is that the anonymous callback method captures the ui_item variable by reference. Since there is only one variable, it always gets whatever was assigned last to the variable.

问题是匿名回调方法通过引用捕获ui_item变量。因为只有一个变量,它总是得到最后分配给变量的东西。

You need to wrap the contents of the for loop in a function that takes i as a parameter, then call the function in the loop. Each call to the wrapper function will create a separate variable, solving the problem.

您需要将for循环的内容封装到一个以i为参数的函数中,然后在循环中调用该函数。每个对包装器函数的调用都将创建一个单独的变量,从而解决问题。

For example:

例如:

function doCheck(i) {
    var split_values = split_files_cb_value_holder[i].split(':');

    var ui_item = split_files_cb_value_holder[i];

    $.ajax({
        type: "POST",
        url: "ds/index.php/playlist/check_folder",
        data: "component_type="+$('#component_type').val()+"&value="+split_values[1],
        success: function(msg)
        {
            console.log(ui_item); //Don't always get the last value
        },
        error: function()
        {
            alert("An error occured while updating. Try again in a while");
        }
    });
}

for (var i = 0; i < split_files_cb_value_holder.length; i++) 
    doCheck(i);

#2


-5  

Turn async off, it will fix the problem i guess. I mean add this: async:false

关闭异步,它将解决我猜想的问题。我的意思是添加这个:async:false