1

So i spend whole two days on trying to understand this, and maybe im still missing something. I declare global variable in callback, which is executed after recieving json, so why still my var is undefined?

$.getJSON("http://ipinfo.io", callbackFuncWithData);

function callbackFuncWithData(data)
{
 response = data;
}
 console.log(response);
  • call another function inside `callbackFuncWithData` where you can write `console.log` that will work – Thamaraiselvam Feb 20 '16 at 11:38
  • The get request takes some time to get the information, the code will run the console.log before it had a chance to change the value. And by the time it changed the value your console.log would have been run already. You either need to do a timeout or you need to move the console.log inside the callback function. – Chris Feb 20 '16 at 12:08

0 Answers0