I am running a loop function with an AJAX request inside it, and the success function on the AJAX need to access a counter variable outside of the loop function, but it wont access it.
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");
}
};
});
As stated in the notes, i need to access the runCount
variable inside the ajax success function, but I cant. It works fine if I access it just outside of the AJAX request.
Just to confirm, the ajax request is working, as it is console logging yes
for each time, so I cant see why it wouldn't increment?