Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript函数变量值_Javascript - Fatal编程技术网

javascript函数变量值

javascript函数变量值,javascript,Javascript,功能是 function getnumber(tt1){ var hy=0; var ic=0; var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.st

功能是

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

混淆同一函数中的hy变量,但值不同。

onreadystatechange
事件将在服务器响应准备好处理时发生

所以在此之前
console.log(hy)已执行。因此其值为0。

因为XHR是异步的