我有这个 jQuery 函数:
var searchResultsCounter = 0;
function searchPerson(filterText){
//code
$.ajax({
type: "GET",
dataType:"json",
url: "res/main.php",
data: { command : "loadPeople",
filter: filterText },
success: function( people ){
for(var i = 0; i< people.length; i++){
//code
}
searchResultsCounter = people.length;
console.log(searchResultsCounter);
}
});
console.log(searchResultsCounter);
return searchResultsCounter;
}
在第一个控制台日志中,我searchResultsCoutner
的值很好,在第二个日志中,它变成了 0。为什么会这样?