0

The function is

function getnumber(tt1){
                var hy=0;
                var ic=0;
                var xhttp = new XMLHttpRequest();

                xhttp.onreadystatechange = function() {
                 if (xhttp.readyState == 4 && xhttp.status == 200) {
                   var Jsonfile= JSON.parse(xhttp.responseText);
                    hy= Jsonfile.total;
                    console.log(hy); // in here it has the value 2
                }
            };
            var url="https://hypothes.is/api/search?user=" + tt1[1] + "&sort=created&order=asc";
            xhttp.open("GET", url, true);
            xhttp.send();
            console.log(hy); // but in here it still 0

confuse about the hy variable in the same function, but value is different.

AntiGMO
  • 1,535
  • 5
  • 23
  • 38

1 Answers1

0

onreadystatechange event, will happen when the server response is ready to be processed.

So before this console.log(hy); is already executed. hence its value is 0.

Sunil B N
  • 4,159
  • 1
  • 31
  • 52