I am calling an api to get the user's country language and storing in localStorage for further use. The data it's on localStorage but I'm getting null value on the first call. The data is only retrieved if I call it again. It seems there is a delay, I am not sure if it's because localStorage, I would appreciate an explanation. How can I get the data right on the first call?
if ((!country_lang) {
$.getJSON('https://api.ipregistry.co/?key=' + api_ipregistry_key, function(data) {
localStorage.setItem('country_lang', data.location.language.code);
});
}
var country_lang = localStorage.getItem('country_lang');
alert(country_lang);