繁体   English   中英

无法从ajax请求内部访问范围变量

[英]Cant access scope variable from inside ajax request

我正在运行一个内部带有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 ,所以我看不到为什么它不增加?

可能有更好的方法,但是一种可靠的方法是制作另一个函数(使用javascript),并使用该函数进行更新,例如:

function bumpRunCount(){
  runCount++;
}

然后从ajax结果中调用该函数。 当然,这取决于runCount的范围。 我只提到那是因为您提到了一个函数,而我没有看到一个函数,因此有可能只显示了该函数的一部分。 如果是这种情况,则需要确保var runCountglobal namespace一部分; 或至少可以访问。

为避免出现此范围问题,请尝试此操作,请尝试解析循环函数中的变量

 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++;


        };
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM