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是异步的