从ajax请求

时间:2018-01-16 10:57:04

标签: javascript jquery ajax

我正在运行一个带有AJAX请求的循环函数,而AJAX上的成功函数需要访问循环函数之外的计数器变量,但它不会访问它。

   var updateCount = 10;
   var runCount = 0;
   $('#main-table .changed').not("[value='0']").closest('tr').each(function(e){
            if($(this).closest('tr').find('.changed').val() == 1){
                var row = $(this).closest('tr');
                var notes = row.find('input[name="notes"]').val();
                var category = row.find('select[name="category"]').val();
                var fileId = row.find('select[name="file"]').val();
                var fileRow = row.find('.file-id').val();
                var quarter_id = row.find('.quarter-id').val();  
                $.ajax({
                    url: '/myurl',
                    type: "POST",
                    data: {_token: CSRF_TOKEN, 'file_row' : fileRow, 'files' : fileId, 'notes': notes, 'category' : category, 'type' : type, 'quarter': quarter_id, 'row_count':updateCount},
                    success: function(data){
                        if(data == 0){


                            console.log("yes");
                            //I need to increment the runcount here, but it wont increment
                            runCount++;


                        }
                    }
                });


               //If i increment the run count here outside the AJAX request, it works
                runCount++;



                if(runCount == updateCount){
                   console.log("Done");
                }
            };
    });

如说明中所述,我需要访问ajax成功函数中的runCount变量,但我不能。如果我在AJAX请求之外访问它,它可以正常工作。

为了确认,ajax请求正在运行,因为每次都是控制台记录yes,所以我不明白为什么它不会增加?

2 个答案:

答案 0 :(得分:0)

可能有更好的方法,但一种确定的方法是制作另一个功能(在javascript中)并用它更新它,如:

=SUMIFS(RawData!G:G,RawData!$D:$D,IF($B$7="","<>",{"SW","SE"}),RawData!$C:$C,$C21,RawData!$E:$E,IF($B$9="","<>",{"A","C"}),RawData!$F:$F,IF($B$10="","<>",{"1","0"}))

然后从ajax结果中调用该函数。那当然取决于function bumpRunCount(){ runCount++; } 的范围。我只提到因为你提到了一个函数,而我没有看到一个函数,所以你可能只展示了函数的一部分。如果是这种情况,您需要确保runCountvar runCount的一部分;或者至少可以访问。

答案 1 :(得分:0)

要避免此范围问题,请尝试解析循环函数内的变量

 var runCount = 0;
   $('#main-table .changed').not("
     [value='0']").closest('tr').each((runCount)=>{
        if($(this).closest('tr').find('.changed').val() == 1){
            var row = $(this).closest('tr');
            var notes = row.find('input[name="notes"]').val();
            var category = row.find('select[name="category"]').val();
            var fileId = row.find('select[name="file"]').val();
            var fileRow = row.find('.file-id').val();
            var quarter_id = row.find('.quarter-id').val();  
            $.ajax({
                url: '/myurl',
                type: "POST",
                data: {_token: CSRF_TOKEN, 'file_row' : fileRow, 'files' : 
                fileId, 'notes': notes, 'category' : category, 'type' : 
                type, 
                'quarter': quarter_id, 'row_count':updateCount},
                success: function(data){
                    if(data == 0){


                        console.log("yes");
                        //I need to increment the runcount here, but it wont increment
                        runCount++;


                    }
                }
            });


           //If i increment the run count here outside the AJAX request, it 
           works
            //runCount++;


        };
});
相关问题