如何在修改其值的函数之外使用全局变量numRes
的修改值?
当我在修改后访问它时,我只得到初始值。
在 How do I change the value of a global variable inside of a function和How do I return the response from an asynchronous call? 问题描述了类似的东西,但它们没有帮助。
提前感谢您的帮助。
google.load('visualization', '1');
// ...
var query = new google.visualization.Query(
'http://www.google.com/fusiontables/gvizdata?tq=' + queryText);
numRes = 0;
query.send(function(response) {
numRes = response.getDataTable().getNumberOfRows();
console.log(numRes + " results are found - inside query.send"); // correct
});
// how to get the same value as three lines above?
console.log(numRes + " results are found"); // not correct, gives 0
// setTimeout(console.log(numRes + " results are found - after timeout"), 10000); // does not help
foo = numRes;
// ... code with foo