javascript - Variable outside function scope -
i have function reads json document, daynames , monthnames visible outside function, date format isn't. inside function correctly prints value outside doesn't update outside value. why dateformat doesn't update outside function?
var daynames = []; var monthnames = []; var dateformat = ""; $.getjson("/scripts/cldr/main/"+ culture + "/ca-gregorian.json", function (json) { $.each(json.main.@(system.threading.thread.currentthread.currentculture.name).dates.calendars.gregorian.days.format.short, function (key, val) { daynames.push(val); }); $.each(json.main.@(system.threading.thread.currentthread.currentculture.name).dates.calendars.gregorian.months["stand-alone"].wide, function (key, val) { monthnames.push(val); }); dateformat = json.main.@(system.threading.thread.currentthread.currentculture.name).dates.calendars.gregorian.dateformats.medium; console.log(dateformat); //output: y-mm-dd }); console.log(dateformat); //output:
the problem $.getjson()
asynchronous call. console.log(datefomrat)
outside function runs before function finishes. code continues while $.getjson()
running dateformat
isn't set when second(outside) console.log()
executes.
Comments
Post a Comment