У меня есть функция, которая читает json document, dayNames и monthNames видна вне функции, но формат даты - нет. Внутри функции он правильно печатает свое значение, но вне его не обновляется внешнее значение. Итак, почему dateFormat не обновляет внешнюю функцию?Переменная внешняя область действия
var dayNames = [];
var monthNames = [];
var dateFormat = "";
$.getJSON("/Scripts/cldr/main/"+ culture + "/ca-gregorian.json", function (json) {
$.each([email protected](System.Threading.Thread.CurrentThread.CurrentCulture.Name).dates.calendars.gregorian.days.format.short, function (key, val) {
dayNames.push(val);
});
$.each([email protected](System.Threading.Thread.CurrentThread.CurrentCulture.Name).dates.calendars.gregorian.months["stand-alone"].wide, function (key, val) {
monthNames.push(val);
});
dateFormat = [email protected](System.Threading.Thread.CurrentThread.CurrentCulture.Name).dates.calendars.gregorian.dateFormats.medium;
console.log(dateFormat); //Output: y-MM-dd
});
console.log(dateFormat); //Output:
Это связано с тем, что 'console.log (dateFormat);' выполняется перед '$ .getJSON()' обратным вызовом –
'$ .getJSON()' is async. 'dateFormat' еще не доступен во внешнем' console.log() ' – Andreas
Причина вне" console.log (dateFormat); " запускается до вашего обратного вызова getJSON. – ashokd