I have a for loop for running some iterations of javascript code and a function within that For Loop. The problem is that the function within my loop isn't receiving the index integer. Here is the code:
for (var i = 0; i < ToggleCount; i++) {
ToggleFolder[i] = gui.addFolder(ToggleDescription[i]);
ToggleChange[i] = { Status: false, Auto: false };
ToggleStatus[i] = ToggleFolder[i].add(ToggleChange[i], 'Status', false).listen();
ToggleAuto[i] = ToggleFolder[i].add(ToggleChange[i], 'Auto', false).listen();
ToggleStatus[i].onFinishChange(function (value, i) {
console.log(i);
$.ajax({
url: 'StatusUpdate.php',
type: 'get',
data: 'ToggleDevice=' + ToggleDescription[i] + '&val=' + value,
success: function (result) { }
});
console.log(ToggleDescription[i]);
});
ToggleFolder[i].open();
}
The problem is that the console.log command is saying that "i" is undefined when it gets within the function. What is my problem?