There is a code in it for() for 2 passes, for each pass an array of data _vars . Inside the loop, I get access to this data _vars[i].param , but in the done() ajax variable i = undefined , a construction like _vars[i] does not plow. How to be?
Theoretically, in the closure, variables are sought in nested functions higher and higher. Is there a function in the callback here and not visible?
Code
function _init() { _loadingImg.show(); var _dataSend = {}, _vars = [{action:8, name: 'service'}, {action:7, name: 'town'}]; for (var i = 0; i < 2; i++) { _dataSend.action = _vars[i].action; $.ajax({ url: 'query.php', type: 'POST', dataType: 'json', data: _dataSend }) .done(function(response) { var _sel = $('<select></select').attr({ class: 'chosen-select', name: _vars[i].name }); for (var j = 0; j < response.length; j++) { _sel.append('<option value="' + response[j].tid + '">' + response[j].name + '</option>') } $('.box.select').prepend(_sel); }) .fail(function() { console.log("error"); }) .always(function() { $('.chosen-select').chosen(); _loadingImg.hide(); }); } } And one more question, in the method of always() there is a disconnection of the loading animation _loadingImg.hide() , how to start it only with the condition of the final execution of both Ajax requests?
i=undefinedand noti==2and because of this_vars[i]==undefined? - Grundyi = undefinedand because of it the wrong conclusion was made: is the function apparently visible in the callback and not visible? - Grundy